1
\$\begingroup\$

I'm developing a game as a learning-process, and since everything I do is new to me, I have alot of problems, usually I can fix it myself but right now I'm stuck and need help.

Link to a recording of my game: http://www.youtube.com/watch?v=nejaMtug09A&feature=youtube_gdata

As you can see in this video I have my joystick working and my animation 80% complete.

!!EDIT : SOLVED!! So first question: How do I get my animation to stop? I have a variable called "dragging" that is set to "true" whenever I move, so it shouldnt be that hard,

when dragging == false (stop animation and display the correct image of my sprite standing still, in the correct direction);

!!EDIT : NOT SOLVED!! My second question: As you can see in the video I get a bullet to spawn when I press the circle in the bottom right corner, I can get it to spawn on my sprite ( or at the end of my sprites gun) if I want, but what I cant do is setting a speed for my bullet, and also I want to be able to have multiple bullets on the screen at once, not just one, with some delay between bullets ofc.

If you can answer anyone of these questions you've made my life alot easier :) Posting code here:

the code for my sprite-animation:

    private static final int BMP_ROWS = 4;
private static final int BMP_COLUMNS = 3;
private int currentFrame = 0;

    public int getAnimationRow() {
    direction = (int) Math.round(controls.getAngle()) % happy.getBmpRows();
    return DIRECTION_TO_ANIMATION_MAP[direction];}

    private void update() {

    currentFrame = ++currentFrame % BMP_COLUMNS;
}


public void draw(Canvas canvas, int x, int y) {
    update();
    int srcX = currentFrame * width;
    int srcY = panel.getAnimationRow() * height;
    Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
    Rect dst = new Rect(x, y, x + width, y + height);
    canvas.drawBitmap(bitmap, src, dst, null);
}

the code for my controls:

    if (event.getAction() == MotionEvent.ACTION_DOWN && (int) event.getX() > 0 && (int) event.getX() < 100
            && (int) event.getY() > 300 && (int) event.getY() < 400) {
        dragging = true;

    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        dragging = false;
        shooting=false;
    }

    if (dragging) {
        // get the pos
        touchingPoint.x = (int) event.getX();
        touchingPoint.y = (int) event.getY();

        double angle = Math.atan2(touchingPoint.y - inity,
                touchingPoint.x - initx) / (Math.PI / 180);
        spriteAngle = Math.atan2(touchingPoint.y - inity,
                touchingPoint.x - initx) / (Math.PI / 2) + 2;


        pointerPosition.y += Math.sin(angle * (Math.PI / 180))
                * (touchingPoint.y / 70);
        pointerPosition.x += Math.cos(angle * (Math.PI / 180))
                * (touchingPoint.y / 70);

    if (event.getAction() == MotionEvent.ACTION_DOWN && (int) event.getX() > 650 && (int) event.getX() < 750
            && (int) event.getY() > 300 && (int) event.getY() < 400){
        shooting = true;
    }

Lastly, my render-method:

    public void render(Canvas canvas) {
    canvas.drawColor(Color.BLUE);
    canvas.drawBitmap(joystick.get_joystickBg(), controls.initx - 50, controls.inity - 50, null);
    happy.draw(canvas, controls.pointerPosition.x, controls.pointerPosition.y);
    canvas.drawBitmap(joystick.get_joystick(), controls.touchingPoint.x - 26, controls.touchingPoint.y - 26, null);
    canvas.drawBitmap(joystick.get_trigger(), controls.triggerX - 50, controls.triggerY - 50, null);

    if(controls.getShooting() == true){
        canvas.drawBitmap(bullet.getStartBullet(), 200, 200, null);
    }

    displayFps(canvas, avgFps);

If you've read all this then you are a very patient person, I posted alot of code but I think you will need it all to be able to help, if you need more let me know and Ill post ASAP :)

Thank you!

Will post the code of the fixed problem down here for any1 that might have the same issue in the future:

Code that fixed my animation-question:

    public void render(Canvas canvas) {
    canvas.drawColor(Color.BLUE);
    canvas.drawBitmap(joystick.get_joystickBg(), controls.initx - 50, controls.inity - 50, null);
    if(controls.getDragging() == true){
        happy.update();
    }else{
        happy.setFrame(1);
    }
    happy.draw(canvas, controls.pointerPosition.x, controls.pointerPosition.y);
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

In your draw() method, you're calling update(), which is incrementing the current animation frame, currentFrame = ++currentFrame % BMP_COLUMNS; You are calling this every frame, regardless if the player is actually moving or not.

You essentially just want to call that update() method only when the player is dragging or providing moving input. Moving the update() call inside the if (dragging) block, and resetting the current animation frame to the first frame in the sprite sheet if not dragging should give you your desired results. Very simple pseduocode would look like this.

if (dragging)
   update();
else
   currentFrame = firstFrame;

As for your bullet question, you essentially want to move the bullet sprite in similar fashion as you do your current player sprite, but instead of relying on user input to provide velocity and direction, you travel the bullet at a steady velocity and given direction each frame. Very very simple pseduocode of firing a bullet when user inputs would look something like this.

if (shotTimer >= minShotTimer) {
    bulletSprite.location += location + velocity;
    shotTimer = 0.0f;
}

Both location and velocity are 2D Vectors. Obviously, that's seriously simplified, but hopefully, it'll nudge you in the right direction.

\$\endgroup\$
8
  • \$\begingroup\$ The part about my update()-method makes me feel so stupid, cant believe I didnt think of that, but I guess sometimes you can stare yourself blind at a problem. About the bullet you did give me some idea yes :) Thank you very much for taking the time to help m8 :) \$\endgroup\$
    – Green_qaue
    Commented Aug 13, 2012 at 22:56
  • \$\begingroup\$ You're welcome! Don't beat yourself up about it, really. We've all been there, staring at the code, looking for the bug that is staring right back at you ;) Just be sure to accept my answer if it's the answer you were looking for. \$\endgroup\$ Commented Aug 13, 2012 at 23:01
  • \$\begingroup\$ will accept ur answer 2morrow when I've started programming again! Still need some help with the bullet, the whole velocity*time is 100% new for me and cant find any good axplenations out there. but thank you very much for the help with my animation! \$\endgroup\$
    – Green_qaue
    Commented Aug 13, 2012 at 23:02
  • \$\begingroup\$ Very good! Let me see if I can update my answer with a better example showcasing the usage of velocity. \$\endgroup\$ Commented Aug 13, 2012 at 23:04
  • \$\begingroup\$ Would be really cool if you could do that :) Will keep trying until you do! thx \$\endgroup\$
    – Green_qaue
    Commented Aug 14, 2012 at 16:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .