2014-03-01 153 views
1

基本上我有一个游戏,我一直在努力使用libGDX。我有一个character,自动前进。而用户点击屏幕/点击,我想character的速度减少。我似乎无法弄清楚如何做到这一点。任何帮助都会很棒。减轻水龙头?

ScrollHandler

package com.kilobolt.GameObjects; 

import com.kilobolt.GameWorld.GameWorld; 
import com.kilobolt.ZBHelpers.AssetLoader; 

public class ScrollHandler { 

    private Grass frontGrass, backGrass; 
    private Pipe pipe1, pipe2, pipe3; 
    public static final int SCROLL_SPEED = -1000; 
    public static final int PIPE_GAP = 49; 

    private GameWorld gameWorld; 

    public ScrollHandler(GameWorld gameWorld, float yPos) { 
     this.gameWorld = gameWorld; 
     frontGrass = new Grass(0, yPos, 143, 11, SCROLL_SPEED); 
     backGrass = new Grass(frontGrass.getTailX(), yPos, 143, 11, 
       SCROLL_SPEED); 

     pipe1 = new Pipe(210, 0, 22, 60, SCROLL_SPEED, yPos); 
     pipe2 = new Pipe(pipe1.getTailX() + PIPE_GAP, 0, 22, 70, SCROLL_SPEED, 
       yPos); 
     pipe3 = new Pipe(pipe2.getTailX() + PIPE_GAP, 0, 22, 60, SCROLL_SPEED, 
       yPos); 
    } 

    public void update(float delta) { 
     // Update our objects 
     frontGrass.update(delta); 
     backGrass.update(delta); 
     pipe1.update(delta); 
     pipe2.update(delta); 
     pipe3.update(delta); 

     // Check if any of the pipes are scrolled left, 
     // and reset accordingly 
     if (pipe1.isScrolledLeft()) { 
      pipe1.reset(pipe3.getTailX() + PIPE_GAP); 
     } else if (pipe2.isScrolledLeft()) { 
      pipe2.reset(pipe1.getTailX() + PIPE_GAP); 

     } else if (pipe3.isScrolledLeft()) { 
      pipe3.reset(pipe2.getTailX() + PIPE_GAP); 
     } 

     // Same with grass 
     if (frontGrass.isScrolledLeft()) { 
      frontGrass.reset(backGrass.getTailX()); 

     } else if (backGrass.isScrolledLeft()) { 
      backGrass.reset(frontGrass.getTailX()); 

     } 
    } 

    public void stop() { 
     frontGrass.stop(); 
     backGrass.stop(); 
     pipe1.stop(); 
     pipe2.stop(); 
     pipe3.stop(); 
    } 

    public boolean collides(Bird bird) { 

     if (!pipe1.isScored() 
       && pipe1.getX() + (pipe1.getWidth()/2) < bird.getX() 
         + bird.getWidth()) { 
      addScore(1); 
      pipe1.setScored(true); 
      AssetLoader.coin.play(); 
     } else if (!pipe2.isScored() 
       && pipe2.getX() + (pipe2.getWidth()/2) < bird.getX() 
         + bird.getWidth()) { 
      addScore(1); 
      pipe2.setScored(true); 
      AssetLoader.coin.play(); 

     } else if (!pipe3.isScored() 
       && pipe3.getX() + (pipe3.getWidth()/2) < bird.getX() 
         + bird.getWidth()) { 
      addScore(1); 
      pipe3.setScored(true); 
      AssetLoader.coin.play(); 

     } 

     return (pipe1.collides(bird) || pipe2.collides(bird) || pipe3 
       .collides(bird)); 
    } 

    private void addScore(int increment) { 
     gameWorld.addScore(increment); 
    } 

    public Grass getFrontGrass() { 
     return frontGrass; 
    } 

    public Grass getBackGrass() { 
     return backGrass; 
    } 

    public Pipe getPipe1() { 
     return pipe1; 
    } 

    public Pipe getPipe2() { 
     return pipe2; 
    } 

    public Pipe getPipe3() { 
     return pipe3; 
    } 

} 

Bird

package com.kilobolt.GameObjects; 

import com.badlogic.gdx.math.Circle; 
import com.badlogic.gdx.math.Vector2; 
import com.kilobolt.ZBHelpers.AssetLoader; 

public class Bird { 

    private Vector2 position; 
    private Vector2 velocity; 
    private Vector2 acceleration; 

    private float rotation; 
    private int width; 
    private int height; 

    private boolean isAlive; 

    private Circle boundingCircle; 

    public Bird(float x, float y, int width, int height) { 
     this.width = width; 
     this.height = height; 
     position = new Vector2(x, y); 
     velocity = new Vector2(0, 0); 
     acceleration = new Vector2(0, 460); 
     boundingCircle = new Circle(); 
     isAlive = true; 
    } 

    public void update(float delta) { 

     velocity.add(acceleration.cpy().scl(delta)); 

     if (velocity.y > 200) { 
      velocity.y = 200; 
     } 

     position.add(velocity.cpy().scl(delta)); 

     // Set the circle's center to be (9, 6) with respect to the bird. 
     // Set the circle's radius to be 6.5f; 
     boundingCircle.set(position.x + 9, position.y + 6, 6.5f); 


     } 



    public boolean isFalling() { 
     return velocity.y > 110; 
    } 

    public boolean shouldntFlap() { 
     return velocity.y > 70 || !isAlive; 
    } 

    public void onClick() { 
     if (isAlive) { 
      AssetLoader.flap.play(); 
      velocity.y = -140; 
     } 
    } 

    public void die() { 
     isAlive = false; 
     velocity.y = 0; 
    } 

    public void decelerate() { 
     acceleration.y = 0; 
    } 

    public float getX() { 
     return position.x; 
    } 

    public float getY() { 
     return position.y; 
    } 

    public float getWidth() { 
     return width; 
    } 

    public float getHeight() { 
     return height; 
    } 

    public float getRotation() { 
     return rotation; 
    } 

    public Circle getBoundingCircle() { 
     return boundingCircle; 
    } 

    public boolean isAlive() { 
     return isAlive; 
    } 
} 
+1

唉,我以为我们可以帮上忙。太糟糕了,我的电脑上没有您的代码副本。抱歉。 –

+0

你可以包含一些你当前的代码吗? –

+0

只是相应的部分? @JustinJasmann – user3366907

回答

0

这里是我的建议:

每当MouseListener检测到点击,有它运行沿playerSpeed -=东西线或playerSpeed = playerSpeed - *some other number*。这样,无论何时发生点击,玩家都会放慢速度。

现在,如果您想要这样做,以便在发生点击时,播放器变慢,再次点击时,播放器会再次加速,使用boolean可以在点击时切换慢速和快速发生。

+0

这是我的'人物'又名'鸟'https://ideone.com/z9kEdF – user3366907

+0

正确,所以哪种方法具体是你有问题,'onClick()'或'减速度>> –

+0

我只是相当新的和不知道该用什么。但是因为我在做Android和iOS,所以'MouseListener'仍然可以工作? – user3366907