简体   繁体   English

跳精灵

[英]Make a sprite jump

I want to make a sprite\\bitmap jump using only android (no game engines). 我只想使用android(没有游戏引擎)来制作一个sprite \\ bitmap。 I wasn't able to find tutorials on how to do so in android using only canvas and views, but I did find a tutorial for xna( http://www.xnadevelopment.com/tutorials/thewizardjumping/thewizardjumping.shtml ), and I tried to recreate it with the tools android offers. 我无法仅使用画布和视图来找到有关如何在android中执行此操作的教程,但确实找到了xna的教程( http://www.xnadevelopment.com/tutorials/thewizardjumping/thewizardjumping.shtml ),并且我试图用android提供的工具重新创建它。 I was able to make the character move left and right using the tutorial code but making it jump just won't work. 我能够使用教程代码使角色左右移动,但是使它跳转只是不起作用。

this is my sprite class: 这是我的精灵课程:

package com.example.spiceup;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.view.KeyEvent;
import android.widget.Toast;

public class Sprite {
      enum State
      {
          Walking, Standing, Jumping
      }
      State mCurrentState = State.Standing;

      Point mDirection;
      int mSpeed = 0;
      Context cc;
      int mPreviousKeyboardState;
    private String spriteName;
    Bitmap sprite;
    private int rows;
    private int rows2;
     int START_POSITION_X = 125;
     int START_POSITION_Y = 245;
     int SPRITE_SPEED = 6;
     int MOVE_UP = -1;
     int MOVE_DOWN = 1;
     int MOVE_LEFT = -1;
     int MOVE_RIGHT = 1;
     Point mStartingPosition;
     int aCurrentKeyboardState;
    private float mScale = 1.0f;
    Point Position;
    public Sprite(String name,Bitmap sprite) {
        this.sprite=sprite;
        this.spriteName=name;
     Position=new Point(150,150);
     mStartingPosition=new Point(150,150);
      mDirection=new Point(0,0);
    }
    public void Update()
    {
        UpdateMovement(aCurrentKeyboardState);
        UpdateJump(aCurrentKeyboardState);


    }
    public void setkeyboard(int keyboard){
         aCurrentKeyboardState = keyboard;
    }
    public void setLastKeyboard(int keyboard){
        mPreviousKeyboardState = keyboard;
   }
    private void UpdateMovement(int aCurrentKeyboardState)
    {
        if (mCurrentState == State.Walking)
        {
            mSpeed = 0;
            mDirection.x = 0;

            if (aCurrentKeyboardState==KeyEvent.KEYCODE_A)
            {
                mSpeed = SPRITE_SPEED;
                mDirection.x = MOVE_LEFT;
            }
            else if(aCurrentKeyboardState==KeyEvent.KEYCODE_D)
            {
                mSpeed = SPRITE_SPEED;
                mDirection.x= MOVE_RIGHT;
            }
            Position.x += mDirection.x * mSpeed;
        }
    }
    private void UpdateJump(int aCurrentKeyboardState)
    {
        if (mCurrentState == State.Walking)
        {
            if (aCurrentKeyboardState==KeyEvent.KEYCODE_SPACE && mPreviousKeyboardState!=KeyEvent.KEYCODE_SPACE)
            {
                Jump();
            }
        }

        if (mCurrentState == State.Jumping)
        {
            if (mStartingPosition.y - Position.y> 150)
            {
                Position.y += mDirection.y * mSpeed;
                mDirection.y = MOVE_DOWN;
            }

            if (Position.y > mStartingPosition.y)
            {
                Position.y = mStartingPosition.y;
                mCurrentState = State.Walking;
            }
        }
    }
    private void Jump()
    {
        if (mCurrentState != State.Jumping)
        {
            mCurrentState = State.Jumping;
            mStartingPosition = Position;
            mDirection.y = MOVE_UP;
            mSpeed = 6;
            Position.y += mDirection.y * mSpeed;
        }
    }
    public void Draw(Canvas c)
    {            
        c.drawBitmap(sprite, Position.x,Position.y, null);
    }
    public void setmCurrentState(State mCurrentState) {
        this.mCurrentState = mCurrentState;
    }
}

this is the surfaceview: 这是surfaceview:

import com.example.spiceup.Sprite.State;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView {
    Context cc;
    Bitmap Sprite;
    Sprite sprite2;
    Handler handlerAnimation100;
    private GameLoopThread gameLoopThread;

    private SurfaceHolder holder;


public GameView(Context c) {
    // TODO Auto-generated constructor stub
super(c);
gameLoopThread = new GameLoopThread(this);
this.cc=c;
this.Sprite=BitmapFactory.decodeResource(getResources(), R.drawable.walk1);
this.Sprite=Bitmap.createScaledBitmap(Sprite, Sprite.getWidth()*2, Sprite.getHeight()*2, false);
sprite2=new Sprite("Spicy",Sprite);
this.requestFocus();
this.setFocusableInTouchMode(true);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {

       @Override
       public void surfaceDestroyed(SurfaceHolder holder) {
              boolean retry = true;
              gameLoopThread.setRunning(false);
              while (retry) {
                     try {
                           gameLoopThread.join();
                           retry = false;
                     } catch (InterruptedException e) {
                     }
              }
       }

       @Override
       public void surfaceCreated(SurfaceHolder holder) {
              gameLoopThread.setRunning(true);
              gameLoopThread.start();
       }

       @Override
       public void surfaceChanged(SurfaceHolder holder, int format,
                     int width, int height) {
       }
});
}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
      canvas.drawColor(Color.BLACK);
      sprite2.Update();
sprite2.Draw(canvas);   
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    sprite2.setkeyboard(keyCode);
    sprite2.setmCurrentState(State.Walking);
    return false;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
       sprite2.setmCurrentState(State.Standing);
       sprite2.setLastKeyboard(keyCode);
    return false;
}
}

if anyone knows where is my error or has a better code to show me I'll be happy, all I'm trying to do is to create a bitmap that moves around and can jump (but also jump while walking) 如果有人知道我的错误在哪里或者有更好的代码告诉我我会很高兴,那么我要做的就是创建一个可以移动并且可以跳跃(但在行走时也可以跳跃)的位图

So I think what is happening in your code is that it's hitting the max height in the game loop and adding back to the y of the sprite. 所以我认为你的代码中发生的事情是它在游戏循环中达到了最大高度并且加回到精灵的y。 Second game loop run it is no longer above or near the max height distance therefore you stop going down and your start position becomes that position in the air. 第二个游戏循环运行它不再高于或接近最大高度距离因此你停止下降并且你的起始位置变成空中的那个位置。 On a third loop through you hit spacebar again and your sprite starts the whole jumping processes over and the same thing happens on the next to loops through or however many it takes to trigger that if statement to get the sprite to start falling. 在第三个循环中你再次点击空格键,你的精灵开始整个跳跃过程,同样的事情发生在下一个循环通过或触发if语句以使精灵开始下降所需的多少。

Good place to start is have a persistent boolean that determines whether or not the sprite is actually done climbing and jumping state should stay true while climbing and falling. 开始的好地方是有一个持久的布尔值来确定精灵是否真的完成攀爬和跳跃状态应该在攀爬和下降时保持真实。 See below. 见下文。

boolean maxJumpAchieved = false;
     if (mCurrentState == State.Jumping)
    {
        if (mStartingPosition.y - Position.y> 150)
        {
            maxJumpAchieved = true;
        }
        if (maxJumpAchieved) {
            mDirection.y = MOVE_DOWN;
            Position.y += mDirection.y * mSpeed;
        }
        if (Position.y > mStartingPosition.y)
        {
            maxJumpAchieved = false;
            Position.y = mStartingPosition.y;
            mCurrentState = State.Walking;
        }
    }

I think this should get you in the right direction but if you have issues let me know and I can edit my answer. 我认为这应该为您提供正确的方向,但是如果您遇到问题,请告诉我,我可以编辑答案。

Another thing to note is don't set the mCurrentState to State.Walking until you know for sure you're on the ground otherwise you could double jump for days. 另外需要注意的是不要将mCurrentState设置为State.Walking,直到你确定你已经在地面上,否则你可以连续几天跳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM