做一个精灵跳 [英] Make a sprite jump

查看:155
本文介绍了做一个精灵跳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想只用安卓(没有游戏引擎),使精灵\\位跳。我无法找到如何只用帆布和意见的Andr​​oid这样做的教程,但我没有找到XNA教程(的 http://www.xnadevelopment.com/tutorials/thewizardjumping/thewizardjumping.shtml ),我试图用工具Android提供了重新创建它。我能够使用教程code,但使其跳是行不通的离开了人物移动和正确的。

I want to make a sprite\bitmap jump using only android (no game engines). 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. I was able to make the character move left and right using the tutorial code but making it jump just won't work.

这是我的精灵类:

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;
    }
}

这是surfaceview:

this is the 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;
}
}

如果有人知道哪里是我的错误,或者有更好的code,告诉我我会很高兴,所有我想要做的是创建一个围绕移动的位图,能跳(也同时跳步行)

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)

推荐答案

所以,我认为什么是你的code发生的是,这是打在游戏循环的最大高度,并重新添加到精灵的y。第二个游戏循环来看,它已不再是高于或接近最大高度距离,因此你停止下跌,你的起始位置成为空气中的那个位置。在第三循环通过你空格键再次击中你的精灵在开始整个跳跃过程,同样的事情发生在下次通过或包含很多需要触发,如果语句来获得精灵开始下降为循环。

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. 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.

良好的开始是有一个持续的布尔值,决定精灵是否实际进行攀爬和跳跃的状态,而攀登和下降应该保持正确的。见下文。

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.

另外要注意的是不要将mCurrentState设置为State.Walking,直到你肯定知道你在地面上,否则你可以为天双跳。

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.

这篇关于做一个精灵跳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆