Results 1 to 2 of 2

Thread: as3 restting my game

  1. #1

    as3 restting my game

    i'm having a bit of trouble figuring how to reset my game. i have a button that will reset when clicked. anyone have any clue what i should do here?

    {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    [SWF(width="800", height="600", backgroundColor="#E6FCFF")] // changing the width, height and bg color of the page

    public class Main extends Sprite
    {
    // creating properties
    private var _sittingDucks:Array = []; //always set your arrays with [] at the top
    public var board:ScoreDisplay;
    private var _waves:WaveBase;

    public function Main()
    {
    // adding the background, and positioning it
    var background:Background = new Background(); // instantiating
    this.addChild(background); // addChild() adds to display list
    background.x = 30; // positiioning left or right
    background.y = 100; // positioning up or down

    for(var i:uint = 0; i < 5; i++) // creates 5 clouds
    {
    //adding the first cloud, and positioning it
    var clouds:Clouds = new Clouds(); // instantiating
    this.addChild(clouds);// addChild() adds to display list
    clouds.x = 130 + Math.random() * 600; //130 to 730
    clouds.y = 230; // positioning 230px
    clouds.speedX = Math.random() * 3; // adding random speeds between each cloud
    clouds.width = clouds.height = 200 * Math.random()//randomly changes the clouds demensions
    }
    //adding the first cloud, and positioning it
    var _waves:Waves = new Waves(); // instantiating
    this.addChild(_waves);// addChild() adds to display list
    _waves.x = 0;// positioning 0px
    _waves.y = 510;// positioning 510px
    _waves.speedX = Math.random() * 3;// adding random speeds between the background wave

    for(var j:uint = 0; j < 8; j++)// creates 8 ducks
    {
    var ducksucks = new Ducks();// instantiating
    this.addChild(ducks);// addChild() adds to display list
    ducks.x = 100 + j * 100;// positioning at 100px plus, spacing out the ducks at 100px
    ducks.y = 475;// positioning 475px
    _sittingDucks.push(ducks); //adding the ducks to the array
    //object.addEventListener(Event.TYPE, Function);
    ducks.addEventListener(MouseEvent.CLICK, ducksDestroy); // event which triggers the click function
    }
    //i tried adding the waves as a forloop like the clouds and ducks, but it never came out right, idk why?
    var _waves2:Waves = new Waves();// instantiating
    this.addChild(_waves2);// addChild() adds to display list
    _waves2.x = 0;// positioning at 0px
    _waves2.y = 520;// positioning at 520px
    _waves2.speedX = Math.random() * 3;// adding random speeds between the foreground wave

    var setting:ForeGround = new ForeGround();// instantiating
    this.addChild(setting);// addChild() adds to display list
    setting.x = 0;// positioning at 0px
    setting.y = 50;// positioning at 50px
    setting.width = 920;// setting the width of the curtains at 920px. it wasn't right as default

    board = new ScoreDisplay();// instantiating
    this.addChild(board);// addChild() adds to display list
    board.x = 570;// positioning at 570px
    board.y = 35;// positioning at 35px
    board.scoreUpdate(8) //this displays the number of ducks from the start

    }

    private function ducksDestroy(event:MouseEvent):void
    {
    //store the ducks we clicked on in a new array
    var clickedDuckucks = Ducks(event.currentTarget);

    //remove it from the ducks array
    //find the address of the ducks we are removing
    var index:uint = _sittingDucks.indexOf(clickedDuck);

    //remove it from the array with splice
    _sittingDucks.splice(index, 1);

    //remove it from my document's display list
    this.removeChild(clickedDuck);

    board.scoreUpdate(_sittingDucks.length);
    //update the score

    //Check if all the ducks are gone
    if (_sittingDucks.length == 0) //meaning, if the amount of ducks is zero, do the code below
    {
    //showing the you win sign after all the ducks are gone
    var win:YouWonSignBase = new YouWonSignBase();// instantiating
    this.addChild(win);// addChild() adds to display list
    win.x = 380;// positioning at 380px
    win.y = 300;// positioning at 300px

    }
    }
    }
    }

  2. #2
    1,391
    posts
    Registered User
    ok - so here, your setting up almost everything within the constructor - this is fine, but it's times like this where you run into issues - it would be better if you were to create a method that instantiates the game scene using basically everything you have in 'Main', then you can call it to 'build' the scene - so you would call it initially in the constructor, then on game over you remove everything from the stage, and call the method again which will reset all of the data

    another approach is to separate the 'ducks' layer, and since nothing else 'changes' in the game, instead of removing everything and replacing it with new, simply put more ducks on the 'duck layer' and reset the score property - so you would separate the loop for the ducks into a method that does this, then instantiate a container instance (like Sprite) at the correct depth between the waves, then you'll be adding the ducks to that container/layer, as opposed to the stage

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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