Results 1 to 2 of 2

Thread: Game return to play type process

  1. #1

    Game return to play type process

    I'm working on designing a game for my web site. Standard Catch&Avoid game, ActionScript 3.0, which I just picked up a few months ago. I tend to get into game design in spring when I teach it in my multimedia class. I'd like to stay with it and develop more and get better at it.

    At any rate, I have a button in the game that becomes visible when a set amount of time has elapsed. You have the choice of either hitting the road and "riding" more or shopping for upgrades to your motorcycle. The "Ride More" button takes you back to the play frame in the timeline, but the player piece no longer moves. At the end of the allotted time, objects continue to appear at the entry point but no longer move. In the first run, the stage is cleared and stuff no longer shows up or moves.


    Any ideas would be helpful. The basics of this came from Rosenzweig's tutorial on the catch/avoid. I followed his AS 2.0 game book for several years and got the Kindle version of his 3.0 game book a few months ago.



    Code:
    package {    import flash.display.*;
        import flash.events.*;
        import flash.text.*;
        import flash.utils.getTimer;
        import flash.utils.Timer;
        import flash.utils.getDefinitionByName;
    
    
        public class JonesRider extends MovieClip {
            var catcher:Catcher;
            var nextObject:Timer;
            var objects:Array = new Array();
    
    
            //score varsz
            var fuel:int=5;
            var score:int=0;
            var upScore:int=5;
            var downScore:int=1;
    
    
            //movement vars
            var catcherSpeed:int=8;
            var leftArrow:Boolean=false;
            var rightArrow:Boolean=false;
            var upArrow:Boolean=false;
            var downArrow:Boolean=false;
            const speed:Number=9.0;
    
    
            //boundaries
            var topBound:int=0;
            var leftBound:int=0;
            var bottomBound:int=500;
            var rightBound:int=700;
            var roadTop:int=130;
            var roadBottom:int=410;
    
    
            //timer vars
            //game start time to measure against current time
            private var gameStartTime:uint;
            //make var to store current time
            private var gameTime:uint;
    
    
            private var gameTimeField:TextField;
    
    
            public function nextRun() {
                var catcher:Catcher;
                var nextObject:Timer;
                var objects:Array = new Array();
                startGame();
            }
    
    
    
    
    
    
            public function startGame() {
                catcher = new Catcher();
                catcher.y=350;
                catcher.x=250;
                fuelBar.scaleX=1;
                addChild(catcher);
                setNextObject();
                addEventListener(Event.ENTER_FRAME, moveObjects);
                stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressedDown);
                stage.addEventListener(KeyboardEvent.KEY_UP,keyPressedUp);
                addEventListener(Event.ENTER_FRAME,fuelDisplay);
    
    
                //get game start Time
                gameTimeField= new TextField();
                gameTimeField.x=150;
                gameTimeField.y=50;
                addChild(gameTimeField);
                gameStartTime=getTimer();
                gameTime=0;
    
    
            }
    
    
            public function fuelDisplay(event:Event) {
                gameTime=getTimer()-gameStartTime;
                fuelBar.scaleX=1-(gameTime/(fuel*1000));
                if (fuelBar.scaleX<0) {
                    stopGame();
                }
            }
    
    
            public function setNextObject() {
                nextObject=new Timer(1000+Math.random()*1000,1);
                nextObject.addEventListener(TimerEvent.TIMER_COMPLETE,newObject);
                nextObject.start();
            }
    
    
            public function newObject(e:Event) {
                var goodObjects:Array=["circle1","circle2"];
                var badObjects:Array=["square1","square2"];
                if (Math.random()<.5) {
                    var r:int=Math.floor(Math.random()*goodObjects.length);
                    var classRef:Class=getDefinitionByName(goodObjects[r]) as Class;
                    var newObject:MovieClip = new classRef();
                    newObject.typestr="good";
                } else {
                    r=Math.floor(Math.random()*badObjects.length);
                    classRef=getDefinitionByName(badObjects[r]) as Class;
                    newObject = new classRef();
                    newObject.typestr="bad";
                }
                newObject.x=rightBound+newObject.width;
                newObject.y=Math.random()*(roadBottom-roadTop)+roadTop;
                addChild(newObject);
                objects.push(newObject);
                setNextObject();
                var objectsNumber:int = (goodObjects.length + badObjects.length);
            }
    
    
            public function moveObjects(e:Event) {
                for (var i:int=objects.length-1; i>=0; i--) {
                    objects[i].x-=speed;
                    if (objects[i].x<leftBound-objects[i].width) {
                        removeChild(objects[i]);
                        objects.splice(i,1);
                    }
                    if (objects[i].hitTestObject(catcher)) {
                        if (objects[i].typestr=="good") {
                            score+=upScore;
                        } else {
                            score-=downScore;
                        }
                        if (score<0) {
                            score=0;
                        }
                        scoreDisplay.text="Score: "+score;
                        removeChild(objects[i]);
                        objects.splice(i,1);
                    }
                    //move the catcher object
                    if (leftArrow) {
                        catcher.x-=catcherSpeed;
                        if (catcher.x<leftBound) {
                            catcher.x=leftBound;
                        }
                    }
                    if (rightArrow) {
                        catcher.x+=catcherSpeed;
                        if (catcher.x>rightBound) {
                            catcher.x=rightBound;
                        }
                    }
                    if (upArrow) {
                        catcher.y-=catcherSpeed;
                        if (catcher.y<roadTop) {
                            catcher.y=roadTop;
                        }
                    }
                    if (downArrow) {
                        catcher.y+=catcherSpeed;
                        if (catcher.y>roadBottom) {
                            catcher.y=roadBottom;
                        }
                    }
                }
            }
            // set arrow variables to true
            public function keyPressedDown(event:KeyboardEvent) {
                if (event.keyCode==37) {
                    leftArrow=true;
                } else if (event.keyCode == 39) {
                    rightArrow=true;
                } else if (event.keyCode == 38) {
                    upArrow=true;
                } else if (event.keyCode == 40) {
                    downArrow=true;
                }
            }
    
    
            // set arrow variables to false
            public function keyPressedUp(event:KeyboardEvent) {
                if (event.keyCode==37) {
                    leftArrow=false;
                } else if (event.keyCode == 39) {
                    rightArrow=false;
                } else if (event.keyCode == 38) {
                    upArrow=false;
                } else if (event.keyCode == 40) {
                    downArrow=false;
                }
            }
            public function stopGame() {
                removeChild(catcher);
                //Choices frame
                gotoAndStop("choices");
    
    
                //set speed to 0
    
    
                //make all the functions stop
    
    
    
    
                //newObject.stop();
                removeEventListener(TimerEvent.TIMER_COMPLETE,newObject);
                removeEventListener(Event.ENTER_FRAME, moveObjects);
                /*removeEventListener(KeyboardEvent.KEY_DOWN,keyPressedDown);
                removeEventListener(KeyboardEvent.KEY_UP,keyPressedUp);*/
                removeEventListener(Event.ENTER_FRAME,fuelDisplay);
                nextObject.stop();
    
    
                //clear the road of all the stuff
                for (var i:int=objects.length-1; i>=0; i--) {
                    removeChild(objects[i]);
                    objects.splice(i,1);
                }
    
    
            }
    
    
        }
    }

  2. #2
    Try to avoid using frames to represent game states. Take a look at the State pattern to switch between the riding/shopping states.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012