四 AndEngine 画线

package com.example.AndEngineExample02;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.primitive.Line;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import java.util.Random;

public class MyActivity extends BaseGameActivity {
private int CAMERA_WIDTH = 720; //摄像机宽度
private int CAMERA_HEIGHT = 480;//摄像机高度
private Camera mCamera; //摄像机对象
private long RANDOM_SEED=1234567890; //随机数种子

@Override
public Engine onLoadEngine() {
//创建摄像机
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
//创建引擎选项
EngineOptions engineOptions=new EngineOptions(true, EngineOptions.ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
//创建引擎并返回它
return new Engine(engineOptions);
}

@Override
public void onLoadResources() {

}

@Override
public Scene onLoadScene() {
//注册到周期更新回调
this.mEngine.registerUpdateHandler(new FPSLogger());
//建立场景对象
final Scene scene = new Scene(1);
//设置背景
scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
//创建随机数对象
Random random=new Random(RANDOM_SEED);
for(int i=0;i<100;i++){
float x1=random.nextFloat()*CAMERA_WIDTH;
float y1=random.nextFloat()*CAMERA_HEIGHT;
float x2=random.nextFloat()*CAMERA_WIDTH;
float y2=random.nextFloat()*CAMERA_HEIGHT;
Line line=new Line(x1,y1,x2,y2);
line.setColor(random.nextFloat(),random.nextFloat(),random.nextFloat());
scene.getTopLayer().addEntity(line);
}
//返回场景对象
return scene;
}

@Override
public void onLoadComplete() {

}
}

posted @ 2013-06-26 18:20  朱颂东  阅读(308)  评论(0编辑  收藏  举报