开发者

Android Animation Question

I am trying to write the logic for an animation sequence and can'开发者_运维百科t seem to get the thing right. What I want to happen is: if the user clicks on the screen, the method takes in the touchEvent coordinates, and then changes the movement variables of a sprite so that the sprite travels to where the user touched the screen. I have my "launch" event setup like this.

 public void launch(float eventX, float eventY) {
    //get the touch event coords and then move the banana to them.
    fire = true;
               //the x and y variables for the sprite
    getX();
    getY();
               //the target x and y variables
    targetX = eventX;
    targetY = eventY;
               //the total distance the two variable have to "travel"
    distanceX = x - targetX;
    distanceY = y - targetY;
               //variables to update the movement
    moveX = distanceX;
    moveY = distanceY;
}

Then, I thought I was supposed to put the movement variables in the update method like this:

 public void update(long gameTime) {
    if(gameTime > frameTicker + framePeriod) {
        frameTicker = gameTime;
        currentFrame++;
        if(currentFrame >= frameNbr){
            currentFrame = 0;

        }
    }
    this.sourceRect.left = currentFrame * spriteWidth;
    this.sourceRect.right = this.sourceRect.left + spriteWidth;
    if(fire == true){
        x = (int) moveX;
        y = (int) moveY;
    }

If the user clicks as it is, the animation shows up like it's supposed to, but then instantaneously goes to the top left corner of the screen or what I have come to understand is (0,0) on a coordinate system. I can't figure out how to slow it down so that it moves at a reasonable space and goes where it is supposed to.


You could put the whole animation in your launch() function if you want.

For instance, at the end of the function something like:

float incrementX = distanceX / 100;
float incrementY = distanceY / 100;
float spriteX = getX();
float spriteY = getY();
bool xDone = false;
bool yDone = false;

while(!(xDone && yDone)) {
  if (distanceX <= spriteX) {
    spriteX += incrementX; // update the sprite's x coordinate as well
  }
  if (distanceY <= spriteY) {
    spriteY += incrementY; // update the sprite's y coordinate as well
  }
  try{ Thread.sleep(10) } catch(Exception e) {}
}

That code relies on the sprite starting at a lower x and y than the event; if that's not the case it needs to be modified.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜