Skip to content

Instantly share code, notes, and snippets.

@CodeLuca
Created October 4, 2014 08:24
Show Gist options
  • Save CodeLuca/c86fa084de31274325cf to your computer and use it in GitHub Desktop.
Save CodeLuca/c86fa084de31274325cf to your computer and use it in GitHub Desktop.
part of worldcamera;
class Play extends State {
bool timerIsOn = false;
Sprite player;
Group<Sprite> bullets;
Timer bulletTimer;
load() {
game.resourceManager.addBitmapData('ship', 'playerShip1_blue.png');
game.resourceManager.addBitmapData('background', 'space-wallpaper-3.jpg');
game.resourceManager.addBitmapData('laserBlue01', 'laser.png');
}
create() {
game.add.background('background');
player = game.add.sprite('ship')
..speed = 300
..x = 300
..y = 1000;
game.camera.follow(player);
bullets = new Group<Sprite>();
for (int i = 0; i < 10; i++) {
bullets.add(game.add.sprite('laserBlue01', addToWorld: false)
//rotation
..speedY = 500
..killOutOfBounds = true);
}
}
update() {
player.stop();
if(player.y > 1000){
player.y = 1000;
}
if(player.y < 200){
player.y = 200;
}
if(player.x > 1700){
player.x = 1700;
}
if(player.x < 200){
player.x = 200;
}
Sprite bullet;
if (game.keyboard.isDown(KeyCode.SPACE)) {
if (timerIsOn) {
return;
} else {
timerIsOn = true;
bulletTimer = new Timer(new Duration(milliseconds: 300), (finish));
player.center();
bullet = bullets.firstWhere((item) => !item.alive)
..x = player.x
..y = player.y - 50
..alive = true;
bullet.move('up');
bullet.addToWorld();
}
}
player.stop();
if (game.keyboard.isDown(KeyCode.W)) {
player.move('forward');
}
if (game.keyboard.isDown(KeyCode.S)) {
player.move('backward');
}
if (game.keyboard.isDown(KeyCode.A)) {
player.move('left');
}
if (game.keyboard.isDown(KeyCode.D)) {
player.move('right');
}
}
finish(){timerIsOn = false;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment