/** 加载精灵 */ public void addSpr() { bs = new BattleScene(); bs.initialize( SystemData.getActivityHeight(), SystemData.getActivityWidth(), SImage.createImage("/res/bg1.jpg")); bs.load(getResources()); SystemData.setCurrentScene(bs); ds1 = new DynamicSprite(10, 10, 1, true); ds2 = new DynamicSprite(10, 10, 2, true); ds4 = new DynamicSprite(x1 - 150, 10, 3, true); ds5 = new DynamicSprite(x1 - 150, 10, 4, true); ds1.load(SImage.createImage("/res/hero1.png")); ds2.load(SImage.createImage("/res/hero2.png")); ds4.load(SImage.createImage("/res/enemy1.png")); ds5.load(SImage.createImage("/res/enemy2.png")); dsList.add(ds1); dsList.add(ds2); dsList.add(ds5); dsList.add(ds4); bs.addSprite( ds1, SImage.createImage("/res/hero1.png"), SystemData.getActivityHeight() / 3, 120); bs.addSprite( ds4, SImage.createImage("/res/enemy1.png"), SystemData.getActivityHeight() / 3, 200); }
/** * 重力感应测试类 * * @author yyt */ public class SensorTest extends Sensor { /** 新建组件层 */ ComponentLevel clevel = new ComponentLevel(); /** 暂停按钮的背景图片 */ SImage[] pause = { SImage.createImage("/res/pause.png"), SImage.createImage("/res/pause1.png"), SImage.createImage("/res/icon3.png") }; /** 按钮位置 */ public int x2 = 0; /** 按钮位置 */ public int y2 = 0; /** 暂停按钮 */ GameButton pauseb = new GameButton(1, pause, x2, y2); /** 用于获得屏幕分辨率 */ DisplayMetrics dm; /** 按钮按下次数 */ int count = 0; /** * 入口方法 * * @param savedInstanceState Bundle对象 */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置屏幕恒为纵向 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 获得屏幕分辨率 dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); SystemData.setCurrentComponentLevel(clevel); pauseb.setActionListener(new PauseListener()); clevel.add(pauseb); pauseb.setVisible(true); } /** * 设置相应操作 * * @param x x坐标 * @param y y坐标 * @param z z坐标 */ public void act(float x, float y, float z) { final int n = 10; final int m = 40; x2 = x2 - (int) x * n; y2 = y2 + (int) y * n; if (x2 <= 0) { x2 = 0; } if (y2 <= 0) { y2 = 0; } if (x2 >= dm.widthPixels - m) { x2 = dm.widthPixels - m; } if (y2 >= dm.heightPixels - m) { y2 = dm.heightPixels - m; } pauseb.setX(x2); pauseb.setY(y2); } /** * 内部类,按钮的监听器 * * @author shadow */ class PauseListener implements GameActionListener { /** * 按钮监听方法 * * @param e event */ @Override public void actionPerformed(UIAdapter ui, MotionEvent e) { count++; if (count % 2 != 0) { close(); } else { start(); } } } }