- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的自上而下游戏中,我怎样才能让我的玩家在穿过婴儿床时发生碰撞?我正在使用 intersectRectangles 。
这是我的代码
Rectangle player = new Rectangle();
Rectangle babycrib = new Rectangle();
Rectangle intersection = new Rectangle();
// Load the sprite sheet as a texture
cat = new Texture(Gdx.files.internal("spriteCatsheet.png"));
catsprite = new Sprite(cat);
player = new Rectangle();
player.x = Gdx.graphics.getWidth() - player.width - 350;
baby = new Texture(Gdx.files.internal("baby.png"));
sprite_baby = new Sprite(baby);
babycrib = new Rectangle();
sprite_baby.setPosition(180,4000);
更新方法
public void update(){
deltaTime = Gdx.graphics.getDeltaTime();
camera.update();
}
在渲染方法中
// check collision
Intersector.intersectRectangles(player, babycrib, intersection);
if(intersection.x > player.x)
//Intersects with right side
if(intersection.y > player.y)
//Intersects with top side
if(intersection.x + intersection.width < player.x + player.width)
//Intersects with left side
if(intersection.y + intersection.height < player.y + player.height)
//Intersects with bottom side
Intersector.overlaps(player,babycrib);
完整代码如下
public class GameScreen implements Screen ,InputProcessor {
final MyGdxGame game;
// Constant rows and columns of the sprite sheet
private static final int FRAME_COLS = 8, FRAME_ROWS = 4;
private boolean peripheralAvailable;
// Objects used
Animation<TextureRegion> walkAnimation; // Must declare frame type (TextureRegion)
Texture left_paw,right_paw;
Texture baby,cat;
SpriteBatch spriteBatch;
Sprite catsprite,sprite_baby;
ImageButton moveBackward,moveForward;
Viewport viewport;
private Stage stage;
private static float fade;
// A variable for tracking elapsed time for the animation
float stateTime;
private TextureRegion myTextureRegion;
TextureRegion textureRegion;
private TextureRegionDrawable myTexRegionDrawable;
OrthographicCamera camera;
Rectangle player = new Rectangle();
Rectangle babycrib = new Rectangle();
Rectangle intersection = new Rectangle();
float deltaTime;
int progressKnobX = 18;
Float fadeTime = 1f;
public GameScreen(final MyGdxGame game) {
this.game = game;
stage = new Stage(new StretchViewport( 720, 1280));
camera = new OrthographicCamera(1280 ,720);
Gdx.input.setCatchBackKey(true);
camera.update();
Gdx.graphics.setContinuousRendering(true);
Gdx.graphics.requestRendering();
camera.setToOrtho(false, 720, 1280);
Gdx.input.setInputProcessor(stage);
spriteBatch = new SpriteBatch();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.input.setInputProcessor( this);
peripheralAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer);
viewport = new ScreenViewport();
baby = new Texture(Gdx.files.internal("equip/baby.png"));
sprite_baby = new Sprite(baby);
babycrib = new Rectangle();
sprite_baby.setPosition(180,4000);
// Load the sprite sheet as a texture
cat = new Texture(Gdx.files.internal("spriteCatsheet.png"));
catsprite = new Sprite(cat);
player = new Rectangle();
player.x = Gdx.graphics.getWidth() - player.width - 350; //250; //550 // 410
// Use the split utility method to create a 2D array of TextureRegions. This is
// possible because this sprite sheet contains frames of equal size and they are
// all aligned.
TextureRegion[][] tmp = TextureRegion.split(cat, cat.getWidth() / FRAME_COLS , cat.getHeight()/ FRAME_ROWS);
// Place the regions into a 1D array in the correct order, starting from the top
// left, going across first. The Animation constructor requires a 1D array.
TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFrames[index++] = tmp[i][j];
}
}
// Initialize the Animation with the frame interval and array of frames
walkAnimation = new Animation<TextureRegion>(0.099f, walkFrames);
// Instantiate a SpriteBatch for drawing and reset the elapsed animation
// time to 0
spriteBatch = new SpriteBatch();
stateTime = 0f;
//left_control
left_paw = new Texture(Gdx.files.internal("left_paw.png"));
myTextureRegion = new TextureRegion(left_paw);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
moveBackward = new ImageButton(myTexRegionDrawable); //Set the button up
moveBackward.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("left_paw.png"))));
//the hover
moveBackward.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("left_paw_hover.png"))));
moveBackward.setPosition(10,25);
stage.addActor(moveBackward); //Add the button to the stage to perform rendering and take input.
Gdx.input.setInputProcessor(stage);
moveBackward.addListener(new InputListener(){
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("Left Button Pressed");
//Start Animation
progressKnobX = progressKnobX - 4;
Gdx.graphics.setContinuousRendering(true);
motionState=MotionState.NONE;
}
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
System.out.print("Released");
Gdx.graphics.setContinuousRendering(false);
motionState=MotionState.DOWN;
return true;
}
});
stage.addActor(moveBackward);
//right_control
right_paw = new Texture(Gdx.files.internal("right_paw.png"));
myTextureRegion = new TextureRegion(right_paw);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
moveForward = new ImageButton(myTexRegionDrawable); //Set the button up
moveForward.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("right_paw.png"))));
//the hover
moveForward.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("right_paw-hover.png"))));
moveForward.setPosition(517,25);
stage.addActor(moveForward); //Add the button to the stage to perform rendering and take input.
Gdx.input.setInputProcessor(stage);
moveForward.addListener(new InputListener(){
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("Right Button Pressed");
progressKnobX = progressKnobX + 4;
motionState=MotionState.NONE;
}
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
motionState=MotionState.UP;
return true;
}
});
stage.addActor(moveForward);
}
public enum State
{
PAUSE,
RUN,
RESUME,
STOPPED
}
private State state = State.RUN;
MotionState motionState=MotionState.NONE;
enum MotionState {
NONE {
@Override
public boolean update(Rectangle player) {
return true;
}
},
UP {
@Override
public boolean update(Rectangle player) {
player.y += 300 * Gdx.graphics.getDeltaTime();
return false;
}
},
DOWN{
@Override
public boolean update(Rectangle player) {
player.y -= 300 * Gdx.graphics.getDeltaTime();
return false;
}
},
LEFT{
@Override
public boolean update(Rectangle player) {
player.x -= 100 * Gdx.graphics.getDeltaTime();
return false;
}
},
RIGHT{
@Override
public boolean update(Rectangle player) {
player.x += 100 * Gdx.graphics.getDeltaTime();
return false;
}
};
public abstract boolean update(Rectangle player);
}
@Override
public void show() {
}
public void update(){
deltaTime = Gdx.graphics.getDeltaTime();
camera.position.x += 10;
camera.position.y += 10;
camera.update();
}
@Override
public void render(float delta) {
// clear previous frame
Gdx.gl.glClearColor(0.294f, 0.294f, 0.294f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen
stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time
camera.update();
update();
spriteBatch.begin();
stateTime += Gdx.graphics.getDeltaTime();
TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true);
camera.position.x = player.getX() + 100; //190
camera.position.y = player.getY() + 180;
camera.position.x = 350;
update();
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.draw(currentFrame,player.x, player.y);
sprite_baby.draw(spriteBatch);
if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) motionState = MotionState.DOWN;
if(Gdx.input.isKeyPressed(Input.Keys.UP)) motionState=MotionState.UP;
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) motionState=MotionState.LEFT;
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) motionState=MotionState.RIGHT;
if(motionState.update(player)) motionState=MotionState.NONE;
// check collision
Intersector.intersectRectangles(player, babycrib, intersection);
if(intersection.x > player.x)
//Intersects with right side
if(intersection.y > player.y)
//Intersects with top side
if(intersection.x + intersection.width < player.x + player.width)
//Intersects with left side
if(intersection.y + intersection.height < player.y + player.height)
//Intersects with bottom side
Intersector.overlaps(player,babycrib);
//Intersects with bottom side
if(!player.overlaps(babycrib)){
Gdx.app.log("babycrib overlaps", "yes");
}
//Mobile acceleration
if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)) {
player.x -= Gdx.input.getAccelerometerX();
}
if (player.x < 0) {
player.x = 0;
player.x += Gdx.graphics.getDeltaTime() *20 *delta;
}
if (player.x > Gdx.graphics.getWidth()-player.getWidth() -150) {
player.x = Gdx.graphics.getWidth()-player.getWidth() -150;
}
if(this.state==State.RESUME) {
switch (state) {
case RUN:
//do suff here
break;
case PAUSE:
break;
case RESUME:
break;
default:
break;
}
}
spriteBatch.end();
stage.act(); //acting a stage to calculate positions of actors etc
stage.draw(); //drawing it to render all
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
camera.update();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public boolean keyDown(int keycode) {
return false;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
@Override
public void hide() {
}
@Override
public void dispose() { // SpriteBatches and Textures must always be disposed
}
}
谁能告诉我矩形碰撞检测的正确实现是什么,没有重叠发生,我是这个框架的新手。谢谢并提前:)
最佳答案
您可以使用 player.contains(babycrib)
进行常规碰撞检测。您也可以为此使用 Intersector
,但这也会计算发生重叠的区域。
if (Intersector.intersectRectangles(player, babycrib, intersection))
{
//player and babycrib are intersecting...
if (intersection.contains(babyRoom))
{
//Collision happened in baby room.
}
}
关于java - 矩形之间的碰撞检测 libgdx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44252997/
我正在用 jQuery Collision 编写这个游戏,它使用键盘按键来移动 div,当一个 div 接触另一个 div 时,它应该防止重叠。 我到底该怎么做? HTML ----
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Java 2D Collision? 嘿,大家好,我有另一篇关于这个问题的帖子刚刚消失了,所以我想我会尝试得到一些关
嘿伙计们,我正在制作一个 2D java 游戏,我正在尝试找出如何制作一个好的碰撞代码。我目前正在使用以下代码: public void checkCollision() { Rect
我的意思是,当我与实体的侧面碰撞并想要跳跃时,我无法向右/向左移动,因为当我与右侧/左侧的实体碰撞时,我有一个标志可以防止这种情况发生,例如所以: 这是我用来检测碰撞的代码: public void
所以我正在运行 collide_mask 检查,以删除与玩家 Sprite 碰撞时的生物实例。它工作得很好。 pygame.sprite.spritecollide(player, mobs, Tru
我正在研究我的砖 block splinter 机,并制作一个适当的碰撞系统,以便使球逻辑地切换方向,我必须检测球与砖 block 的哪一侧碰撞。这是我当前的脚本: int sprite_collid
我做了一个类似颜色切换的游戏。唯一的问题是玩家与每种颜色发生碰撞...... 这是我从github上获取的代码: https://github.com/prometheon/MLNimbleNinja
测试我的游戏,当用户和怪物发生碰撞时,我希望弹出警报但没有成功: function die() { for (var i = 0; i < monster.length; i++) { i
我对 vector 很陌生,这是我第一次真正使用它们进行碰撞检查。这是我的项目,我对如何实现碰撞感到困惑。我目前的碰撞检查和响应代码似乎是……糟糕的设计。 这是我的代码: for(auto it =
我是 javascript 的新手,正在尝试找出如何与球和木板发生碰撞,这将停止游戏并提醒玩家“你输了”。但我只想让红球击中木板,让蓝球不接触地继续前进。这是我正在处理的代码。 (我不介意你是否可以帮
很抱歉提出奇怪的问题,我还是 Android 编程的新手。 我有以下代码: import android.content.DialogInterface.OnClickListener; import
我有 6 个 UIImageView,每个都连接到 UIPanGestureRecognizer,它们都连接到相同的方法。方法是: - (IBAction)handlePan:(UIPanGestur
我想根据某些对象的轴对齐边界框检查视锥体,以粗略检查这些对象是否在视野中。速度不是什么大问题。 最佳答案 我发现构建视锥体的世界空间模型并检查与它的 bbox 碰撞是错误的方法。 一个更简单的方法是以
我项目中的所有这些代码都运行良好,但我遇到了一些问题。当飞机接触到屏幕的边界时,它会在接触后开始旋转。我不知道如何让它在碰到屏幕边界时不旋转。只有在我使用时才会出现这个问题: plane.physic
在应用程序启动时,我在后台线程中删除旧的 CoreData 行,下面是我的代码。我的问题类似于城市街道问题。所以,我有两个实体,Street 和 City,我有一个关系 City > Street,因
我试图不接触穴居人和其他带有碰撞位掩码的图像,但我的穴居人击中了一切。 func addCaveManBitMasks(){ caveManNode.physicsBody?.category
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
如何在 PyGame 中找到字符和图像之间的碰撞?我已经从图像中绘制了一个玩家,并从瓷砖中绘制了墙壁,那么我如何检测这些碰撞? 最佳答案 如果你使用pygame Rect类来表示对象的边界,您可以使用
我正在使用 ftok() 为 C 应用程序使用的共享内存段生成标识符。我有问题,在一个盒子上我与 root 使用的标识符发生冲突。在这种情况下,我可以通过破解代码来修复它,但我想要一个更强大的解决方案
这个问题在这里已经有了答案: JavaScript: Collision detection (10 个回答) 10 个月前关闭。 检测 2 个物体(墙壁)碰撞的好方法。是的,不仅仅是检测,还有进一步
我是一名优秀的程序员,十分优秀!