public void Act(SpriteBatchObject sprite, long elapsedTime) { // 如果主角与地图上其它对象发生碰撞(以下分别验证) if (game.hero.IsCollision(sprite)) { // 与敌人 if (sprite is Enemy) { Enemy e = (Enemy)sprite; if (game.hero.Y() < e.Y()) { game.hero.SetForceJump(true); game.hero.Jump(); game.RemoveTileObject(e); } else { game.Damage(); } // 与金币 } else if (sprite is Coin) { Coin coin = (Coin)sprite; game.RemoveTileObject(coin); // 与加速道具 } else if (sprite is Accelerator) { game.RemoveTileObject(sprite); Accelerator accelerator = (Accelerator)sprite; accelerator.Use(game.hero); // 与二次弹跳道具 } else if (sprite is JumperTwo) { game.RemoveTileObject(sprite); JumperTwo jumperTwo = (JumperTwo)sprite; jumperTwo.Use(game.hero); } } }
public override void Create() { // 以指定图片创建动画 this.coinAnimation = Animation.GetDefaultAnimation("assets/coin.png", 32, 32, 200); this.enemyAnimation = Animation.GetDefaultAnimation("assets/enemy.gif", 32, 32, 200, LColor.black); this.accelAnimation = Animation.GetDefaultAnimation( "assets/accelerator.gif", 32, 32, 200); this.jumpertwoAnimation = Animation.GetDefaultAnimation( "assets/jumper_two.gif", 32, 32, 200); // 注销Screen时释放下列资源 PutReleases(coinAnimation, enemyAnimation, accelAnimation, jumpertwoAnimation, hero); // 加载一张由字符串形成的地图(如果不使用此方式加载,则默认使用标准的数组地图) TileMap indexMap = TileMap.LoadCharsMap("assets/map.chr", 32, 32); // 如果有配置好的LTexturePack文件,可于此注入 // indexMap.setImagePack(file); // 设定无法穿越的区域(如果不设置此项,所有索引不等于"-1"的区域都可以穿越) indexMap.SetLimit(new int[] { 'B', 'C', 'i', 'c' }); indexMap.PutTile('B', "assets/block.png"); int imgId = indexMap.PutTile('C', "assets/coin_block.gif"); // 因为两块瓦片对应同一地图字符,所以此处使用了已载入的图片索引 indexMap.PutTile('i', imgId); indexMap.PutTile('c', "assets/coin_block2.gif"); // 加载此地图到窗体中 putTileMap(indexMap); // 获得地图对应的二维数组 int[][] maps = indexMap.GetMap(); int w = indexMap.GetRow(); int h = indexMap.GetCol(); // 遍历二维数组地图,并以此为基础添加角色到窗体之上 for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { switch (maps[j][i]) { case 'o': Coin coin = new Coin(indexMap.TilesToPixelsX(i), indexMap.TilesToPixelsY(j), new Animation( coinAnimation), indexMap); AddTileObject(coin); break; case 'k': Enemy enemy = new Enemy(indexMap.TilesToPixelsX(i), indexMap.TilesToPixelsY(j), new Animation( enemyAnimation), indexMap); AddTileObject(enemy); break; case 'a': Accelerator accelerator = new Accelerator( indexMap.TilesToPixelsX(i), indexMap.TilesToPixelsY(j), new Animation( accelAnimation), indexMap); AddTileObject(accelerator); break; case 'j': JumperTwo jump = new JumperTwo(indexMap.TilesToPixelsX(i), indexMap.TilesToPixelsY(j), new Animation( jumpertwoAnimation), indexMap); AddTileObject(jump); break; } } } // 获得主角动作图 Animation animation = Animation.GetDefaultAnimation("assets/hero.png", 20, 20, 150, LColor.black); // 在像素坐标位置(192,32)放置角色,大小为32x32,动画为针对hero.png的分解图 hero = AddJumpObject(192, 32, 32, 32, animation); // 让地图跟随指定对象产生移动(无论插入有多少张数组地图,此跟随默认对所有地图生效) // 另外请注意,此处能产生跟随的对像是任意LObject,并不局限于游戏角色。 Follow(hero); // 监听跳跃事件 hero.listener = new JumpI(indexMap, enemyAnimation); AddActionKey(Key.LEFT, new GoLeftKey()); AddActionKey(Key.RIGHT, new GoRightKey()); AddActionKey(Key.UP, new GoJumpKey()); if (LSystem.type != LSystem.ApplicationType.JavaSE) { LPad pad = new LPad(10, 180); pad.SetListener(new PadClick(this)); Add(pad); } this.updateListener =new GameUpdateListener(this); }
public override void Create() { // 以指定图片创建动画 this.coinAnimation = Animation.GetDefaultAnimation("assets/coin.png", 32, 32, 200); this.enemyAnimation = Animation.GetDefaultAnimation("assets/enemy.gif", 32, 32, 200, LColor.black); this.accelAnimation = Animation.GetDefaultAnimation( "assets/accelerator.gif", 32, 32, 200); this.jumpertwoAnimation = Animation.GetDefaultAnimation( "assets/jumper_two.gif", 32, 32, 200); // 注销Screen时释放下列资源 PutReleases(coinAnimation, enemyAnimation, accelAnimation, jumpertwoAnimation, hero); // 加载一张由字符串形成的地图(如果不使用此方式加载,则默认使用标准的数组地图) TileMap indexMap = TileMap.LoadCharsMap("assets/map.chr", 32, 32); // 如果有配置好的LTexturePack文件,可于此注入 // indexMap.setImagePack(file); // 设定无法穿越的区域(如果不设置此项,所有索引不等于"-1"的区域都可以穿越) indexMap.SetLimit(new int[] { 'B', 'C', 'i', 'c' }); indexMap.PutTile('B', "assets/block.png"); int imgId = indexMap.PutTile('C', "assets/coin_block.gif"); // 因为两块瓦片对应同一地图字符,所以此处使用了已载入的图片索引 indexMap.PutTile('i', imgId); indexMap.PutTile('c', "assets/coin_block2.gif"); // 加载此地图到窗体中 putTileMap(indexMap); // 获得地图对应的二维数组 int[][] maps = indexMap.GetMap(); int w = indexMap.GetRow(); int h = indexMap.GetCol(); // 遍历二维数组地图,并以此为基础添加角色到窗体之上 for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { switch (maps[j][i]) { case 'o': Coin coin = new Coin(indexMap.TilesToPixelsX(i), indexMap.TilesToPixelsY(j), new Animation( coinAnimation), indexMap); AddTileObject(coin); break; case 'k': Enemy enemy = new Enemy(indexMap.TilesToPixelsX(i), indexMap.TilesToPixelsY(j), new Animation( enemyAnimation), indexMap); AddTileObject(enemy); break; case 'a': Accelerator accelerator = new Accelerator( indexMap.TilesToPixelsX(i), indexMap.TilesToPixelsY(j), new Animation( accelAnimation), indexMap); AddTileObject(accelerator); break; case 'j': JumperTwo jump = new JumperTwo(indexMap.TilesToPixelsX(i), indexMap.TilesToPixelsY(j), new Animation( jumpertwoAnimation), indexMap); AddTileObject(jump); break; } } } // 获得主角动作图 Animation animation = Animation.GetDefaultAnimation("assets/hero.png", 20, 20, 150, LColor.black); // 在像素坐标位置(192,32)放置角色,大小为32x32,动画为针对hero.png的分解图 hero = AddJumpObject(192, 32, 32, 32, animation); // 让地图跟随指定对象产生移动(无论插入有多少张数组地图,此跟随默认对所有地图生效) // 另外请注意,此处能产生跟随的对像是任意LObject,并不局限于游戏角色。 Follow(hero); // 监听跳跃事件 hero.listener = new JumpI(indexMap, enemyAnimation); AddActionKey(Key.LEFT, new GoLeftKey()); AddActionKey(Key.RIGHT, new GoRightKey()); AddActionKey(Key.UP, new GoJumpKey()); if (LSystem.type != LSystem.ApplicationType.JavaSE) { LPad pad = new LPad(10, 180); pad.SetListener(new PadClick(this)); Add(pad); } this.updateListener = new GameUpdateListener(this); }