MyPlayerSprite.java
上传用户:pyly5030
上传日期:2021-09-17
资源大小:174k
文件大小:2k
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- import javax.microedition.lcdui.game.Sprite;
- public class MyPlayerSprite extends Sprite {
-
- public static final int moveSpeed = 5;
- private int x,y;
- private int scnWidth,scnHeight;
- private int frameWidth,frameHeight;
- private int frame;
- private int lives;
-
- public MyPlayerSprite(Image image, int frameWidth, int frameHeight) {
- super(image, frameWidth, frameHeight);
- }
- public MyPlayerSprite(Image image, int frameWidth, int frameHeight,int scnWidth, int scnHeight) {
- super(image,frameWidth,frameHeight);
- this.scnWidth = scnWidth;
- this.scnHeight = scnHeight;
- this.frameWidth = frameWidth;
- this.frameHeight = frameHeight;
- x = scnWidth/2;
- y = scnHeight/2;
- this.frame = 1;
- this.lives = 3;
- }
-
- public Sprite fire(Image bullets){
- Sprite bullet = new Sprite(bullets,2,2);
- getXY();
- bullet.setPosition(x+bullet.getWidth()/2+this.getWidth()/2, y);
- bullet.setFrame(0);
- return bullet;
- }
-
- public void startPosition(){
- setPosition(scnWidth/2, scnHeight/2);
- }
-
- public void moveLeft(){
- getXY();
- if(x - moveSpeed > 0)
- move(moveSpeed*-1,0);
- }
- public void moveRight(){
- getXY();
- if(x + moveSpeed + frameWidth< scnWidth)
- move(moveSpeed,0);
- }
- public void moveUp(){
- getXY();
- if(y-moveSpeed>0)
- move(0,moveSpeed*-1);
- }
- public void moveDown(){
- getXY();
- if(y+moveSpeed+frameHeight<scnHeight)
- move(0,moveSpeed);
- }
- public int getLives() {
- return lives;
- }
- public void setLives(int lives) {
- this.lives = lives;
- }
-
- public void display(Graphics g){
- this.setFrame(frame);
- this.paint(g);
- }
-
- private void getXY(){
- x = getX();
- y = getY();
- }
- }