项目地址:http://code.google.com/p/andengine/
LineExample源码:
package org.anddev.andengine.examples; import java.util.Random; 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.EngineOptions.ScreenOrientation; 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; /** * @author Nicolas Gramlich * @since 11:54:51 - 03.04.2010 */ public class LineExample extends BaseExample { // =========================================================== // Constants // =========================================================== /* Initializing the Random generator produces a comparable result over different versions. */ private static final long RANDOM_SEED = 1234567890; private static final int CAMERA_WIDTH = 720; private static final int CAMERA_HEIGHT = 480; private static final int LINE_COUNT = 100; // =========================================================== // Fields // =========================================================== private Camera mCamera; // =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public Engine onLoadEngine() { this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera)); } @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)); final Random random = new Random(RANDOM_SEED); for(int i = 0; i < LINE_COUNT; i++) { final float x1 = random.nextFloat() * CAMERA_WIDTH; final float x2 = random.nextFloat() * CAMERA_WIDTH; final float y1 = random.nextFloat() * CAMERA_HEIGHT; final float y2 = random.nextFloat() * CAMERA_HEIGHT; final float lineWidth = random.nextFloat() * 5; final Line line = new Line(x1, y1, x2, y2, lineWidth); line.setColor(random.nextFloat(), random.nextFloat(), random.nextFloat()); scene.getTopLayer().addEntity(line); } return scene; } @Override public void onLoadComplete() { } // =========================================================== // Methods // =========================================================== // =========================================================== // Inner and Anonymous Classes // =========================================================== }
在onLoadEngine里,Camera是一个重点,EngineOption也是一个。
Camera:A Camera defines the rectangle of the scene that is drawn on the screen, as not the whole scene is visible all the time. Usually there is one Camera per Scene, except for the SplitScreenEngines. There are subclasses that allow zooming and smooth position changes of the Camera.
Camera定义了在屏幕上的矩形Scene,因为不是整个Scene都是一直可见的。通常一个Camera对应一个Scene,除非是SplitScreenEngines.它通过Camera允许放大,平移和变换位置。
EngineOption:EngineOption需要四个参数,分别是 FullScreen(是否全屏) ScreenOrientaiton(横屏或竖屏) RatioResolutionPolicy(放大缩小比例) Camera(前文的Camera)
RatioResolutionPolicy 需要new RatioResolutionPolicy(int width, int height)按比例修正画面大小,以适应屏幕大小
在onLoadScene()经常会看到registerUpdateHandler()和Scene的用法
registerUpdateHandler()暂略
Scence:The Scene class is the root container for all objects to be drawn on the screen. A Scene has a specific amount of Layers, which themselves can contain a (fixed or dynamic) amount of Entities. There are subclasses, like the CameraScene/HUD/MenuScene that are drawing themselves to the same position of the Scene no matter where the camera is positioned to.
Scene类是屏幕里面的所有对象的根容器。Scene拥有指定的Layer,这些 Layer能拥有多个(固定或动态的)实例。它的子类如CameraScene/HUD/MenuScene都是画在同一个位置上不管camera的位置是哪里。
http://android.toolib.net/index.html
http://androidbox.sinaapp.com/
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ touchLabel.text = @"Touches Began"; } - (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ touchLabel.text = @"Touches Cancelled"; } - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ touchLabel.text = @"Touches Stopped"; } - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ touchLabel.text = @"Drag detected"; }
需要注意的是:在Interface Builder中,必须将view的属性Interaction里的两项User Interaction Enabled和Multiple Touch都勾上。