dimanche 22 novembre 2015

How to set only one object to spawn with a timer?

I am currently a beginner trying to learn programming with as3 so bare with me. I have this code so far and my game repeatedly spawns the Food2 (golden apple) but it does not stop. My game also does not pause on frame 1 to allow me to press the button in order to start the game. Here is my code so far:

package{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event; 
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;

public class Main extends MovieClip{
    const speed:int = 10;
    var score:int;
    var vx:int;
    var vy:int;
    var gFood:Food;
    var gFood2:Food2;
    var head:SnakePart;
    var SnakeDirection:String;
    var snake:Array;
    var upPressed:Boolean = false;
    var downPressed:Boolean = false;
    var leftPressed:Boolean = false;
    var rightPressed:Boolean = false;
    var myTimer:Timer = new Timer(10000,1);

    public function Main(){
        gotoAndStop(1);
        init();
    }
    function init():void {
        vx = 1; vy = 0;
        score = 0;
        snake = new Array();
        SnakeDirection = "";
        addFood();
        myTimer.start();
        head = new SnakePart();
        head.x = stage.stageWidth/2;
        head.y = stage.stageHeight/2;
        snake.push(head);
        addChild(head);
        stage.addEventListener(KeyboardEvent.KEY_UP , onKeyUp);
        start1.addEventListener(MouseEvent.CLICK, startgame);
        help1.addEventListener(MouseEvent.CLICK, helpplayer);
        stage.addEventListener(KeyboardEvent.KEY_DOWN , onKeyDown);
        addEventListener(Event.ENTER_FRAME , onEnterFrame);
    }
    private function startgame(event:MouseEvent):void {
        gotoAndPlay(3);
        stop();
    }
    private function helpplayer(event:MouseEvent):void {
        gotoAndPlay(2);
        stop();
    }
    function addFood():void {
        gFood = new Food();
        gFood.x = 50 + Math.random()*(stage.stageWidth-100);
        gFood.y = 50 + Math.random()*(stage.stageHeight-100);
        addChild(gFood);
    }
    function addFood2():void {
        gFood2 = new Food2();
        gFood2.x = 70 + Math.random()*(stage.stageWidth-100);
        gFood2.y = 70 + Math.random()*(stage.stageHeight-100);
        addChild(gFood2);
    }
    function reset():void {
        score = 0;
        removeChild(gFood);
        removeChild(gFood2);
        myTimer.stop();
        addFood();
        head.x = stage.stageWidth/2;
        head.y = stage.stageHeight/2;
        vx = 1;vy = 0;
        for(var i = snake.length-1;i>0;--i){
            removeChild(snake[i]);
            snake.splice(i,1);
        }
    }
    function onKeyDown(event:KeyboardEvent):void{
        if(event.keyCode == Keyboard.LEFT){
           SnakeDirection = "left";
        }else if (event.keyCode == Keyboard.RIGHT) {
           SnakeDirection = "right";
        }else if (event.keyCode == Keyboard.UP) {
            SnakeDirection = "up";
        }else if (event.keyCode == Keyboard.DOWN) {
            SnakeDirection = "down";
        }
    }
    function onKeyUp(event:KeyboardEvent):void {
        if(event.keyCode == Keyboard.LEFT) {
            SnakeDirection = "";
        }else if(event.keyCode == Keyboard.RIGHT) {
            SnakeDirection = "";
        }else if(event.keyCode == Keyboard.UP ) {
            SnakeDirection = "";
        }else if(event.keyCode == Keyboard.DOWN){
            SnakeDirection = "";
        }
    }
    function onEnterFrame(event:Event):void {
        Timerfood();
        txtScore.text = "Score:" + String(score);
        if(SnakeDirection == "left" && vx != 1) {
            vx = -1;
            vy = 0;
        }
        else if(SnakeDirection == "right" && vx != -1) {
            vx = 1;
            vy = 0;
        }
        else if(SnakeDirection == "up" && vy != 1) {
            vx = 0;
            vy = -1;
        }
        else if(SnakeDirection == "down" && vy != -1) {
            vx = 0;
            vy = 1;
        }
        if(head.x - head.width/2 <= 0){
            score = 0;
            reset();
        }
        if(head.x + head.width/2 >= stage.stageWidth){
            score = 0;
            reset();
        }
        if(head.y - head.height/2 <= 0){
            score = 0;
            reset();
        }
        if(head.y + head.height/2 >= stage.stageHeight){
            score = 0;
            reset();
        }

        for(var i = snake.length-1;i>0;--i){
            snake[i].x = snake[i-1].x;
            snake[i].y = snake[i-1].y;
        }
        head.x += vx*speed;
        head.y += vy*speed;

        for(var i = snake.length-1;i>=1;--i){
            if(snake[0].x == snake[i].x && snake[0].y == snake[i].y){
                reset();
                break;
            }
        }
        if(head.hitTestObject(gFood)){
            score += 1;
            removeChild(gFood);
            addFood();
            var bodyPart = new SnakePart();
            bodyPart.x = snake[snake.length - 1].x;
            bodyPart.y = snake[snake.length - 1].y;
            snake.push(bodyPart);
            addChild(bodyPart);
        }
        if(head.hitTestObject(gFood2)){
            score += 2;
            removeChild(gFood2);
            addFood2();
            var bodyPart = new SnakePart();
            bodyPart.x = snake[snake.length - 1].x;
            bodyPart.y = snake[snake.length - 1].y;
            snake.push(bodyPart);
            addChild(bodyPart);
        }
        function Keyboard_a(event:KeyboardEvent):void {
            if (event.keyCode == 82) {
            resetgame();
            }
        }
        function resetgame():void {
            score = 0;
            if (contains(head))
                removeChild(head);
            init();
        }
        }
        function Timerfood():void {
            var myTimer:Timer = new Timer(10000,1);
            myTimer.addEventListener(TimerEvent.TIMER, timerListener);
            function timerListener(e:TimerEvent):void{
                trace("Timer is Trigered");
                addFood2();
            }
            myTimer.start();
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire