A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: Score counter in AS3

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    18

    Question Score counter in AS3

    Hi everyone,

    The game I'm making is a shooter: alienships fly across the screen and when I click them, they die.

    I want to have a dynamic text that keeps score of the number of ships I kill, but I'm not managing to do it.

    I have a "Main class" and an "Alienship class" in my game. This code is in the "Alienship class":

    function Alienship():void
    {
    addEventListener(MouseEvent.CLICK,whenclick);
    }

    private function whenclick(e:MouseEvent)
    {
    trace("You're dead\n");
    parent.removeChild(this);
    }

    It removes the alienship off the screen. But how can I have a variable that increments on each killing?

    Thanks

    Chiapa

  2. #2
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    I would just add a "score" variable to the Main class and then increment score on "whenclick" with either: score++; OR score += scoreIncrement;

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    Hi steven, thanks for your reply

    problem is, if I do that the score will increment each time I shoot, and not each time I shoot a spaceship.

    Sorry, I tried doing that in the class Spaceship but the score is always one, each time I shoot the spaceship, because it's initialized for each spaceship as score:int = 0;

    Can you help me, please?

  4. #4
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    Well the score variable should only be initialized once preferably in the Main Class, like you have score:int = 0;

    Instead of using whenClick, just add the score increment code to the AS that handles the spaceship click. I don't know all your code, but the principle is the same. Initialize score and then increment on hit. Just add score increment to alienship event listener.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    Well, the event listener is in the class "Spaceship", not in "Main", and therefore I can't use that variable in the class Spaceship (or I don't know how to)

    The code:

    Two classes: Main and Nave (meaning Spaceship);

    -------------MAIN-------------------
    package
    {
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.ui.Mouse;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    import flash.events.Event;
    import flash.media.Sound;
    import flash.net.*;
    import flash.media.SoundChannel;

    public class Main extends MovieClip
    {
    var total:int = 200;
    var naves = new Array;
    var tempo:Timer;
    var tempo2:Timer;

    var pontos:int = 0;
    var tempojogo:Timer = new Timer(1000,1);
    var musica_fundo:Sound = new Sound();
    var tiro:Sound = new Sound();

    function Main():void
    {
    play_btn.addEventListener(MouseEvent.CLICK, jogar);

    function jogar(evt:MouseEvent):void
    {
    if (currentFrame == 1)
    {
    gotoAndStop(totalFrames);
    }
    else

    Mouse.hide();
    stage.addEventListener(MouseEvent.MOUSE_MOVE,mexe_ mira);
    stage.addEventListener(MouseEvent.CLICK,som);

    init_naves();
    tempo = new Timer(700,total);
    tempo.addEventListener(TimerEvent.TIMER,voa);
    tempo.start();

    tempo2 = new Timer(900,total);
    tempo2.addEventListener(TimerEvent.TIMER,voa2);
    tempo2.start();

    tempojogo.addEventListener(TimerEvent.TIMER,contad or);
    tempojogo.start();
    tiro.load(new URLRequest("SFX/tiro4mp.mp3"));
    }
    }

    function som(event:MouseEvent)
    {
    tiro.play(0);

    }

    function contador(event:TimerEvent)
    {
    contador_txt.text = String(tempojogo.currentCount);
    }

    private function voa(e:TimerEvent):void
    {
    var nave = naves.pop();
    nave.voa();
    }

    private function voa2(e:TimerEvent):void
    {
    var nave = naves.pop();
    nave.voa2();
    }

    private function init_naves():void
    {
    var nave1;
    var nave2;

    for (var i = 0; i < total; i++)
    {
    nave1 = new Nave ;
    nave1.x = -30;
    nave1.y = Math.floor(Math.random() * this.stage.stageHeight);

    this.addChild(nave1);
    naves.push(nave1);
    }

    musica_fundo.load(new URLRequest("Musica/Carmina.mp3"));
    musica_fundo.play();
    }

    private function mexe_mira(event:MouseEvent)
    {
    Mouse.hide();
    mira.x = mouseX;
    mira.y = mouseY;
    }
    }
    }
    --------------------------------------
    -------------NAVE-------------------

    package
    {
    import flash.display.MovieClip;
    import flash.events.*;
    import flash.utils.Timer;
    import flash.media.Sound;
    import flash.net.*;
    import flash.media.SoundChannel;

    public class Nave extends MovieClip
    {
    var speed_x:int = 10;
    var speed_x2:int = 12;
    var speed_y:int = 2;
    var speed_y2:int = -2;
    // var pontos:int = 0;

    function Nave():void
    {
    this.addEventListener(MouseEvent.CLICK, contapontos);
    }

    function contapontos(e:MouseEvent):void
    {
    // pontos = pontos + 1;
    // trace(pontos);
    }

    private function quandoClique(e:MouseEvent)
    {
    trace("Já foste!\n");
    parent.removeChild(this);

    }

    public function voa():void
    {
    this.addEventListener(Event.ENTER_FRAME, passa);
    addEventListener(MouseEvent.CLICK,quandoClique);
    }

    public function voa2():void
    {
    this.addEventListener(Event.ENTER_FRAME, passa2);
    addEventListener(MouseEvent.CLICK,quandoClique);
    }

    private function quandoFrame(e:Event)
    {
    x += speed_x;
    y += speed_y;
    }

    private function passa(e:Event):void
    {
    this.x= (this.x)+speed_x;
    this.y = (this.y)+speed_y;

    }

    private function passa2(e:Event):void
    {
    this.x= (this.x)+speed_x2;
    this.y = (this.y)+speed_y2;
    }
    }
    }
    --------------------------------------

    The names of the functions and variables are in Portuguese but if you want I can translate them to English, so you can understand better.

    Thanks, Chiapa

  6. #6
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    To make a global variable use "public" as in: public var score:int = 0;
    Declare this in your main class and it should work. Then manipulate from there.

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    I declared it public, in the Main class, and in the spaceship class, in the function that removes the spaceships when I click them, the code to increment the points variable. This is the error I got:

    D:\Chiapa\Escola\Engenharia Informática\Tecnologias Multimédia\Flash\Chiapa game 2\Nave.as, Line 25 1120: Access of undefined property pontos.

    Pontos means points and I tried:
    points = points +1; trace(pontos);

    in the same function that removes the spaceships off the screen
    Last edited by Chiapa; 07-09-2012 at 08:12 PM.

  8. #8
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    It seems like you're not declaring it properly. And you've tried exactly:
    Code:
    public var pontos:int = 0;
    Only place it in the Main class and try to trace pontos.

  9. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    I can't get it to work: here's some code of the main class:

    public class Main extends MovieClip
    {
    var total:int = 200;
    var naves = new Array;
    var tempo:Timer;
    var tempo2:Timer;

    public var pontos:int = 0;

    var tempojogo:Timer = new Timer(1000,1);
    var musica_fundo:Sound = new Sound();
    var tiro:Sound = new Sound();
    ...
    ...


    And some code of the spaceship class, where I use the variable "pontos":

    private function quandoClique(e:MouseEvent)
    {
    trace("You're dead!\n");
    parent.removeChild(this);
    pontos = pontos + 1;
    trace(pontos);
    }


    However, I get that error, it says that I can't access the variable pontos.

    Help...

  10. #10
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    PHP Code:
    package
    {
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.ui.Mouse;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    import flash.events.Event;
    import flash.media.Sound;
    import flash.net.*;
    import flash.media.SoundChannel;

    public class 
    Main extends MovieClip
    {
    var 
    total:int 200;
    var 
    naves = new Array;
    var 
    tempo:Timer;
    var 
    tempo2:Timer;

    var 
    pontos:int 0;
    var 
    tempojogo:Timer = new Timer(1000,1);
    var 
    musica_fundo:Sound = new Sound();
    var 
    tiro:Sound = new Sound();

    function 
    Main():void
    {
    play_btn.addEventListener(MouseEvent.CLICKjogar);

    function 
    jogar(evt:MouseEvent):void
    {
    if (
    currentFrame == 1)
    {
    gotoAndStop(totalFrames);
    }
    else

    Mouse.hide();
    stage.addEventListener(MouseEvent.MOUSE_MOVE,mexe_ mira);
    stage.addEventListener(MouseEvent.CLICK,som);

    init_naves();
    tempo = new Timer(700,total);
    tempo.addEventListener(TimerEvent.TIMER,voa);
    tempo.start();

    tempo2 = new Timer(900,total);
    tempo2.addEventListener(TimerEvent.TIMER,voa2);
    tempo2.start();

    tempojogo.addEventListener(TimerEvent.TIMER,contad or);
    tempojogo.start();
    tiro.load(new URLRequest("SFX/tiro4mp.mp3"));
    }
    }

    function 
    som(event:MouseEvent)
    {
    tiro.play(0);

    }

    function 
    contador(event:TimerEvent)
    {
    contador_txt.text String(tempojogo.currentCount);
    }

    private function 
    voa(e:TimerEvent):void
    {
    var 
    nave naves.pop();
    nave.voa();
    }

    private function 
    voa2(e:TimerEvent):void
    {
    var 
    nave naves.pop();
    nave.voa2();
    }

    private function 
    init_naves():void
    {
    var 
    nave1;
    var 
    nave2;

    for (var 
    0totali++)
    {
    nave1 = new Nave ;
    nave1.= -30;
    nave1.Math.floor(Math.random() * this.stage.stageHeight);

    this.addChild(nave1);
    naves.push(nave1);
    }

    musica_fundo.load(new URLRequest("Musica/Carmina.mp3"));
    musica_fundo.play();
    }

    private function 
    mexe_mira(event:MouseEvent)
    {
    Mouse.hide();
    mira.mouseX;
    mira.mouseY;
    }
    }
    }
    ------------------------------- 
    This is the Spaceship class:


    PHP Code:
    package
    {
    import flash.display.MovieClip;
    import flash.events.*;
    import flash.utils.Timer;
    import flash.media.Sound;
    import flash.net.*;
    import flash.media.SoundChannel;

    public class 
    Nave extends MovieClip
    {
    var 
    speed_x:int 10;
    var 
    speed_x2:int 12;
    var 
    speed_y:int 2;
    var 
    speed_y2:int = -2;
    // var pontos:int = 0;

    function Nave():void
    {
    this.addEventListener(MouseEvent.CLICKcontapontos);
    }

    function 
    contapontos(e:MouseEvent):void
    {
    // pontos = pontos + 1;
    // trace(pontos);
    }

    private function 
    quandoClique(e:MouseEvent)
    {
    trace("Já foste!\n");
    parent.removeChild(this);
    // pontos = pontos + 1 ;
    // trace(pontos);
    }

    public function 
    voa():void
    {
    this.addEventListener(Event.ENTER_FRAMEpassa);
    addEventListener(MouseEvent.CLICK,quandoClique);
    }

    public function 
    voa2():void
    {
    this.addEventListener(Event.ENTER_FRAMEpassa2);
    addEventListener(MouseEvent.CLICK,quandoClique);

    }

    private function 
    quandoFrame(e:Event)
    {
    += speed_x;
    += speed_y;
    }

    private function 
    passa(e:Event):void
    {
    this.x= (this.x)+speed_x;
    this.= (this.y)+speed_y;

    }

    private function 
    passa2(e:Event):void
    {
    this.x= (this.x)+speed_x2;
    this.= (this.y)+speed_y2;
    }
    }


  11. #11
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    Ok just tested this out. Try the following.

    public static var pontos:int = 0;

    And reference by using Class.variable, ie "trace(Main.pontos);"

  12. #12
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    Thanks Steven

    I had already tried that and it worked. The static variable worked fine, someone in another forum suggested it first but thanks anyway.

    Chiapa

  13. #13
    Senior Member Steven FN's Avatar
    Join Date
    Mar 2010
    Location
    CA, USA
    Posts
    276
    Glad you got it working. Sorry, i thought it was just something on your end, but it was the "static" keyword that was missing.

    I'm not one to use external AS files, i like to keep it all in the FLA or just Main class if possible. Happy flashing.

  14. #14
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    Yeah, I had that thought too, working on the fla file or merging the classes in one.

    Thanks for your help and trouble

    Chiapa

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