package com.example.android.firstflixelgame;
//remember to import java states first, then Flixel
import java.util.ArrayList;
// change import to org.flixel.* instead of org.flixel.FlxState
import android.view.*;
import org.flixel.*;
public class GameState extends FlxState
{
protected ArrayList levelBlocks = new ArrayList();
protected ArrayList enemies = new ArrayList();
protected ArrayList playerBullets = new ArrayList();
protected Player player = null;
protected FlxSound hurt = FlxG.play(R.raw.hurt);
protected FlxSound button = FlxG.play(R.raw.button);
public GameState()
{
//contain sprites to playing field
levelBlocks.add(this.add(new FlxBlock (0, 640-16, 640, 16).loadGraphic(R.drawable.tech_tiles))) ;
levelBlocks.add(this.add(new FlxBlock(0, 0, 640, 16).loadGraphic(R.drawable.tech_tiles))) ;
levelBlocks.add(this.add(new FlxBlock(0, 16, 16, 640-32).loadGraphic(R.drawable.tech_tiles))) ;
levelBlocks.add(this.add(new FlxBlock(640-16, 16, 16, 640-32).loadGraphic(R.drawable.tech_tiles)));
// draw blocks
for(int i=0; i < 4; ++i)
{
levelBlocks.add(this.add(new FlxBlock(320 - 48 - 32*i,640 - 48 - 48*i,320 , 16).loadGraphic(R.drawable.tech_tiles)));
}
//draw enemies
for(int i = 0; i < 4; ++i)
{
enemies.add(this.add(new Enemy(320 - 48 - 32*i, 640 - 48 - 48*i, 320)));
}
//draw bullets - up to 8
for(int i = 0; i < 8; ++i)
playerBullets.add(this.add(new Bullet()));
player = new Player(playerBullets);
this.add(player);
FlxG.follow(player, 2.5f);
FlxG.followAdjust(0.05f, 0.0f);
FlxG.followBounds(0, 0, 640, 640);
FlxG.playMusic(R.raw.mode) ;
}
public void update()
{
super.update();
FlxG.collideArrayList(levelBlocks, player);
FlxG.overlapArrayList(enemies, player, new FlxCollideListener()
{
public void Collide(FlxCore object1, FlxCore object2)
{
FlxG.play(R.raw.hurt);
player.kill();
FlxG.fade(0xffd8eba2,3, new FlxFadeListener()
{
public void fadeComplete()
{
FlxG.switchState(MenuState.class);
}
}
);
}
}
);
FlxG.collideArrayLists(playerBullets, levelBlocks);
FlxG.overlapArrayLists(playerBullets, enemies, new FlxCollideListener()
{
public void Collide(FlxCore bullet, FlxCore enemy)
{
((FlxSprite)bullet).hurt(0);
((FlxSprite)enemy).hurt(1);
}
}
);
//check to see if Menu is Pressed to start over
if(FlxG.keys.justPressed(KeyEvent.KEYCODE_MENU))
{
FlxG.play(R.raw.button);
player.kill();
FlxG.fade(0xffd8eba2,3, new FlxFadeListener()
{
public void fadeComplete()
{
FlxG.switchState(MenuState.class);
}
}
);
}
//check to see if volume went up or down manually arg
if(FlxG.keys.justPressed(KeyEvent.KEYCODE_VOLUME_UP))
{
//turn volume up activity
FlxG.setVolume((float) (FlxG.getVolume() + 0.025));
}
if(FlxG.keys.justPressed(KeyEvent.KEYCODE_VOLUME_DOWN))
{
//turn volume down activity
FlxG.setVolume((float) (FlxG.getVolume() - 0.025));
}
if(FlxG.keys.justPressed(KeyEvent.KEYCODE_CAMERA))
{
//mute volume if camera key is pressed
FlxG.setVolume(0);
}
//check to see if BACKSPACE is pressed
if(FlxG.keys.justPressed(KeyEvent.KEYCODE_BACK))
{
//some stuff to kill my code
System.exit(0);
}
}
}
|