使用hEngine做了一个小游戏Demo,制作过程中发现了引擎中的一些小Bug,已经更正。
代码如下:
package com.example.fly;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import com.hanyeah.HEngine;
import com.hanyeah.display.HBitmap;
import com.hanyeah.display.HSprite;
import com.hanyeah.display.HSpriteSheet;
import com.hanyeah.display.HStage;
import com.hanyeah.display.HTextField;
import com.hanyeah.events.HEvent;
import com.hanyeah.events.HIListener;
import com.hanyeah.events.HTouchEvent;
import com.hanyeah.geom.HPoint;
import com.hanyeah.geom.HRectangle;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
private static HStage stage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
//初始化引擎和stage
if(stage!=null){
HEngine hengine=new HEngine(this);
hengine.stage=stage;
setContentView(hengine);
return;
}
HEngine hengine=new HEngine(this);
setContentView(hengine);
stage=hengine.stage;
stage.frameRate=30;
stage.addEventListener(HEvent.STAGE_CREATED, new HIListener() {
@Override
public boolean execute(HEvent arg0) {
// TODO Auto-generated method stub
init();
return false;
}
});
}
private Bitmap bmp_plane;
private Bitmap bmp_bullet;
private Bitmap bmp_ball;
private HSprite bulletCon;
private HSprite ballCon;
private HSprite plane;
private HPoint planeSize;
private double stageWidth;
private double stageHeight;
private ListbulletList;
private ListballList;
private long t=0;
private int bulletT=0;
private int ballT=0;
private HPoint targetPoint=new HPoint();
private double bulletSpeed=10;
private double ballSpeed=5;
private double planeSpeed=5;
private HPoint bulletSize;
private HPoint ballSize;
private HTextField scoreTf;
private HTextField lifeTf;
private HTextField fpsTf;
private int score=0;
private int life=10000;
private void init(){
//准备位图
bmp_plane=BitmapFactory.decodeResource(this.getResources(), R.drawable.plane);
bmp_bullet=BitmapFactory.decodeResource(this.getResources(), R.drawable.bullet);
bmp_ball=BitmapFactory.decodeResource(this.getResources(), R.drawable.ball);
//变量
stageWidth=stage.getStageWidth();
stageHeight=stage.getStageHeight();
bulletList=new ArrayList();
ballList=new ArrayList();
//创建容器
ballCon=new HSprite();
stage.addChild(ballCon);
bulletCon=new HSprite();
stage.addChild(bulletCon);
//创建飞机
HBitmap planeBmp=new HBitmap(bmp_plane);
plane=new HSprite();
plane.addChild(planeBmp);
planeSize=planeBmp.getSize();
planeBmp.x=-planeSize.x/2;
planeBmp.y=-planeSize.y/2;
stage.addChild(plane);
plane.x=stageWidth/2;
plane.y=stageHeight-planeSize.y;
//文本
scoreTf=new HTextField();
scoreTf.x=20;
scoreTf.y=50;
scoreTf.width=300;
scoreTf.height=50;
scoreTf.color=Color.GREEN;
scoreTf.size=30;
scoreTf.lineHeight=45;
scoreTf.text="得分:"+score;
stage.addChild(scoreTf);
lifeTf=new HTextField();
lifeTf.x=350;
lifeTf.y=50;
lifeTf.width=300;
lifeTf.height=50;
lifeTf.color=Color.RED;
lifeTf.size=30;
lifeTf.lineHeight=45;
lifeTf.text="生命值:"+life;
stage.addChild(lifeTf);
fpsTf=new HTextField();
fpsTf.x=50;
fpsTf.y=0;
fpsTf.width=300;
fpsTf.height=50;
fpsTf.color=Color.RED;
fpsTf.size=30;
fpsTf.lineHeight=45;
fpsTf.text="fps:"+30;
stage.addChild(fpsTf);
//
targetPoint.x=plane.x;
targetPoint.y=plane.y;
Log.e("hanyeah", targetPoint.toString());
stage.addEventListener(HEvent.ENTER_FRAME, new HIListener() {
@Override
public boolean execute(HEvent arg0) {
// TODO Auto-generated method stub
enterFameHandler();
return false;
}
});
stage.addEventListener(HTouchEvent.TOUCH_START, new HIListener() {
@Override
public boolean execute(HEvent arg0) {
// TODO Auto-generated method stub
targetPoint.x=stage.mouseX;
targetPoint.y=stage.mouseY;
return false;
}
});
}
private void enterFameHandler(){
fpsTf.text="fps:"+stage.getFPS();
t++;
bulletT++;
ballT++;
/*
double dis=HPoint.distance(targetPoint, new HPoint(plane.x,plane.y));
double speedX=planeSpeed*(targetPoint.x-plane.x)/ dis;
double speedY=planeSpeed*(targetPoint.y-plane.y)/ dis;
plane.x+=speedX;
plane.y=speedY;
*/
plane.x=targetPoint.x;
//plane.y=targetPoint.y;
/*
stage.graphics.clear();
Path path=new Path();
Paint paint=new Paint();
paint.setColor(0xffff0000);
*/
//更新子弹位置
for(int i=bulletList.size()-1;i>=0;i--){
HSprite bullet=bulletList.get(i);
bullet.y-=bulletSpeed;
if(bullet.y=0;i--){
HSpriteSheet ball=ballList.get(i);
ball.y+=ballSpeed;
if(ball.y>stageHeight+ballSize.y/2){
ball.destroy();
ballCon.removeChild(ball);
ballList.remove(i);
}
//HRectangle rect=ball.getRect();
//path.addRect((float)rect.x, (float)rect.y, (float)(rect.x+rect.width), (float)(rect.y+rect.height),Path.Direction.CCW);
}
//HRectangle rect=plane.getRect();
//path.addRect((float)rect.x, (float)rect.y, (float)(rect.x+rect.width), (float)(rect.y+rect.height),Path.Direction.CCW);
//stage.graphics.addPath(path, paint);
//检测子弹和球的碰撞
for(int i=bulletList.size()-1;i>=0;i--){
HSprite bullet=bulletList.get(i);
HRectangle rect1=bullet.getRect();
for(int j=ballList.size()-1;j>=0;j--){
HSpriteSheet ball=ballList.get(j);
HRectangle rect2=ball.getRect();
if(rect1.intersects(rect2)){//碰撞
if(ball.getCurrentFrame()==ball.getTotalFrames()-1){
ball.destroy();
ballCon.removeChild(ball);
ballList.remove(j);
score++;
scoreTf.text="得分:"+score;
}
else{
ball.nextFrame();
}
bulletCon.removeChild(bullet);
bulletList.remove(i);
break;
}
}
}
//检测球和飞机的碰撞
HRectangle rect1=plane.getRect();
for(int i=ballList.size()-1;i>=0;i--){
HSpriteSheet ball=ballList.get(i);
HRectangle rect2=ball.getRect();
if(rect1.intersects(rect2)){
life--;
lifeTf.text="生命值:"+life;
ballCon.removeChild(ball);
ballList.remove(i);
}
}
//发射子弹
if(bulletT>=15){
bulletT=0;
HBitmap bulletBmp=new HBitmap(bmp_bullet);
if(bulletSize==null)bulletSize=bulletBmp.getSize();
bulletBmp.x=-bulletSize.x/2;
bulletBmp.y=-bulletSize.y/2;
HSprite bullet=new HSprite();
bullet.addChild(bulletBmp);
bulletCon.addChild(bullet);
bullet.x=plane.x;
bullet.y=plane.y;
bulletList.add(bullet);
}
//产生球
if(ballT>50){
ballT=0;
String jsonStr="{\"frames\":[[0,0,89,92,0,42.55,90.5],[89,0,89,92,0,42.55,90.5],[0,92,89,92,0,42.55,90.5]],\"animations\":{\"normal\":[0,1,2,2,2]}}";
JSONObject jsonObj=null;
try {
jsonObj = new JSONObject(jsonStr);
HSpriteSheet ball=new HSpriteSheet(bmp_ball,jsonObj);
ball.gotoAndStop(0);
if(ballSize==null) ballSize=ball.getSize();
ballCon.addChild(ball);
ball.x=ballSize.x/2+(Math.random()*stageWidth-ballSize.x);
ball.y=-ballSize.y/2;
ballList.add(ball);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}可通过下载下面的压缩包得到图片素材。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。