[CS3] Score counter in a shooter game
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [CS3] Score counter in a shooter game

  1. #1
    Junior Member
    Join Date
    Nov 2008
    Posts
    3

    [CS3] Score counter in a shooter game

    I figured that this question would be too basic for the AS3 section, so here it goes.

    Right now, I have a simple shooting game set up. 25 monsters appear on the screen, one after another, until no monsters are left. If you hit one before it disappears, the score increases. If you don't, the score stays the same.

    In the actions frame, I placed an empty dynamic text box named score, and set it equal to 0 (declared in the main actions frame). But, seeing as though the monster actions are coded into the library item itself, do I have to declare score, and the score increase there instead?

    The file is attached so you see what I'm talking about. Any additional help would be greatly appreciated.

    Thanks.
    Attached Files Attached Files

  2. #2
    Junior Member
    Join Date
    Nov 2008
    Posts
    25
    Hi,

    We can use DispatchEvent to communicate events from movie clips to the main timeline. This is how I would do it.

    In the main timeline, add:

    Change

    Code:
    var score:Number = 0;
    to

    Code:
    var scorenum:Number = 0;
    As it is bad to name the variable the same as the movie clip.

    Add this to the main timeline.

    Code:
    function addScore(e:Event):void	{
    	scorenum +=1;
    	score.text = (scorenum.toString());
    }
    Change

    Code:
    function addMonsters(event:TimerEvent):void
    {
    	var monster:MovieClip;
    	monster = new Monster;
    	monster.x = Math.random() * stage.stageWidth;
    	monster.y = Math.random() * stage.stageHeight;
    	monsterHolder.addChild(monster);
    }
    to

    Code:
    function addMonsters(event:TimerEvent):void
    {
    	var monster:MovieClip;
    	monster = new Monster;
    	monster.x = Math.random() * stage.stageWidth;
    	monster.y = Math.random() * stage.stageHeight;
    	monster.addEventListener("my event", addScore);
    	monsterHolder.addChild(monster);
    }
    Now go into the monster movie clip

    Change

    Code:
    function removeMonster(event:MotionEvent):void
    {
    	this_animator.removeEventListener(MotionEvent.MOTION_END, damage);
    	this.parent.removeChild(this);
    }
    to

    Code:
    function removeMonster(event:MotionEvent):void
    {
    	this_animator.removeEventListener(MotionEvent.MOTION_END, damage);
    	this.parent.removeChild(this);
    	dispatchEvent(new Event("my event"));
    }
    Works for me

    P.S. - You have some 1009's in that code, in this.parent.removeChild(this). Good luck with that.
    Last edited by the_j0k3r; 11-25-2008 at 02:33 AM.

  3. #3
    Junior Member
    Join Date
    Nov 2008
    Posts
    3
    I figured that it was this simple. Thanks.

    No 1009 errors for me.

  4. #4
    Junior Member
    Join Date
    Mar 2009
    Posts
    21
    Finally!! I have been trying to figure out how to increase a score when my char moves over an object and thanks to this I've sorted it.

  5. #5
    I'm a Medical Professional!
    Join Date
    Dec 2006
    Location
    Philadelphia, PA
    Posts
    11
    This absolutely just saved my butt.

    I couldn't figure out:

    Code:
    score.text = (scorenum.toString());

    I kept using:

    Code:
    score.text = String(scorenum);
    And slamming my head against the desk.

    Thanks!
    -----------------------------------------------
    - Jay

    www.TooMuchMetal.net

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center

Poll by Flashkit.com