首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

2.凹凸曼之无聊一天-小喽啰登场

2012-10-21 
2.凹凸曼之无聊一天----------小喽啰登场?????? 上次说到为凹凸曼搭了个舞台以解救其无聊的一天,但是谁想

2.凹凸曼之无聊一天----------小喽啰登场



2.凹凸曼之无聊一天-小喽啰登场
?
2.凹凸曼之无聊一天-小喽啰登场
?

???? 上次说到为凹凸曼搭了个舞台以解救其无聊的一天,但是谁想呗一个小喽啰捷足先登,我们的英雄会抓狂吗···········

暂且晾着,先说说这厮是哪里来的。

????? 我们首先创建帧类,用以保存单幅图片,正所谓循环播放图片才产生了动画,故最基本的就是这动画里每一帧。
有点罗嗦,还是先看码?。

1.AnimationFrame.java

import java.awt.image.BufferedImage;/*每个动画的一帧*/public class AnimateFrame {private long duration;//每帧的持续时间,以毫秒计。private BufferedImage image;//每帧显示的图片。public AnimateFrame(BufferedImage image){this(image, 40);//默认50毫秒}public AnimateFrame(BufferedImage image,int duration  ){this.duration=duration;this.image = image;}public long getDuration() {return duration;}public void setDuration(long duration) {this.duration = duration;}public BufferedImage getImage() {return image;}public void setImage(BufferedImage image) {this.image = image;}}

?
简单吧,就是保存每帧的图片和播放时间,以后根据需要还会添加个别属性。
2.Animation.java

import java.awt.image.BufferedImage;/*整个动画*/public class Animation {private AnimateFrame[] frames;//存储所有帧private int currentFrameIndex;//当前帧索引private long countTime;//运行了多久。private int width;private int height;public int getWidth() {return width;}public int getHeight() {return height;}public Animation(int numFrames){this.frames = new AnimateFrame[numFrames];this.currentFrameIndex=0;this.countTime=0L;}//添加帧public void addFrame(AnimateFrame frame){frames[currentFrameIndex++]=frame;if(currentFrameIndex==frames.length){currentFrameIndex=0;}if(width==0||height==0){width = frame.getImage().getWidth();height = frame.getImage().getHeight();}}//重置动画public void reset(){currentFrameIndex=0;countTime=0L;}public void nextFrame(){currentFrameIndex++;countTime=0L;}//形成动画public void animate(long passedtime){int numFrames = frames.length;if(numFrames!=1){//只有一帧就不动。if(currentFrameIndex==numFrames-1){//到底了,重头播放reset();}else{countTime+=passedtime;//时间过去多久了。if(countTime>=getCurrentFrame().getDuration()){//如果时间超过当前帧的DURATION,就播放下一帧;同时重新计时。nextFrame();}}}}public AnimateFrame getCurrentFrame(){return frames[currentFrameIndex];}}

?这个东西保存了所有帧,让它能循环播放图片形成动画。里面有些写的累赘,大家可自行修改。

3.Sprite.java

import java.awt.Color;import java.awt.Graphics;/*一切舞台上的元素*/public class Sprite {int x;int y;//舞台上位置float v;int width;int height;boolean canMoveLeft;boolean canMoveRight;boolean canMoveUp;boolean canMoveDown;private 凹凸曼的舞台 stage ;private Animation animation;public Sprite(凹凸曼的舞台 stage){this(0, 0,0.3f,stage);}public Sprite(int x,int y,float v,凹凸曼的舞台 stage){this(x,y,v,null,stage);}public Sprite(int x,int y,float v,Animation animation,凹凸曼的舞台 stage){this.x = x;this.y = y;this.v = v;this.width=40;this.height=40;this.animation = animation;if(animation!=null){this.width = animation.getWidth();this.height = animation.getHeight();}this.stage = stage;}//更新坐标,运动状态等public void update(long passedtime){float s = v*passedtime;//过去时间内的位移if(canMoveLeft)x-=s;if(canMoveDown)y+=s;if(canMoveRight)x+=s;if(canMoveUp)y-=s;checkCollisionWithStage();if(animation!=null)animation.animate(passedtime);}public void checkCollisionWithStage(){if(x<0)x=0;if(x+width>stage.WIDTH)x=stage.WIDTH-width;if(y<0)y=0;if(y+height+40>stage.HEIGHT)y=stage.HEIGHT-height-40;//飞机不能在最下面,不合常理,所以减40,具体看图片}public void draw(Graphics g){if(animation==null){g.setColor(Color.green);g.fillRect(x, y, width, height);}else{g.drawImage(animation.getCurrentFrame().getImage(), x,y,null);}}public Animation getAnimation() {return animation;}public void setAnimation(Animation animation) {this.animation = animation;this.width = animation.getWidth();this.height = animation.getHeight();}}

?
?号称是舞台上的元素,这个小喽啰就是。可以给他设置动画,让它有模有样。其实就是包装了一下。

4.Plane.java

import java.awt.event.KeyEvent;import java.awt.image.BufferedImage;public class Plane extends Sprite{public Plane(凹凸曼的舞台 stage){super(stage);initAnimation();}//把图片读进plane设置动画public void initAnimation(){BufferedImage[] planes = ImageResource.planeImages;Animation a  = new Animation(20);for(int i =0;i<planes.length;i++){AnimateFrame af = new AnimateFrame(planes[i]);a.addFrame(af);}setAnimation(a);}public void handlePressEvent(KeyEvent e){int keycode = e.getKeyCode();switch (keycode) {case KeyEvent.VK_W:this.canMoveUp=true;break;case KeyEvent.VK_S:this.canMoveDown=true;break;case KeyEvent.VK_A:this.canMoveLeft=true;break;case KeyEvent.VK_D:this.canMoveRight=true;break;}}public void handleReleaseEvent(KeyEvent e){int keycode = e.getKeyCode();switch (keycode) {case KeyEvent.VK_W:this.canMoveUp=false;break;case KeyEvent.VK_S:this.canMoveDown=false;break;case KeyEvent.VK_A:this.canMoveLeft=false;break;case KeyEvent.VK_D:this.canMoveRight=false;break;}}}

?这个是小喽啰,他有些特殊的东西。但他终究是元素。之后我们的小喽啰就擅自跑的舞台上了。。在凹凸曼的舞台上添加。

好了,话说凹凸曼已经不耐烦了,他想干什么呢下回分解

?

热点排行