actionscript 3 - variable in class not returning updated value -
actionscript 3 - variable in class not returning updated value -
i'm relatively new actionscript 3, , 1 has me stumped. here's class;
package { import flash.display.movieclip; import flash.events.mouseevent; import flash.events.event; public class clicker extends movieclip { public var clicks:uint; public var string:string; public function clicker() { // constructor code addeventlistener(event.added_to_stage, addedtostagehandler); addeventlistener(event.added_to_stage, aligncentre); string = "clicks: "; clicks = 5; // can alter here } public function clicked(e:mouseevent):void{ clicks++; trace(clicks); // outputs updated value here } public function aligncentre(e:event):void{ x = stage.stagewidth / 2 - width/2; y = stage.stageheight / 2 - height/2; } public function addedtostagehandler(e:event):void{ this.stage.addeventlistener(mouseevent.click, clicked); } public function get_clicks():uint{ trace(clicks); // gives me whatever initialise in constructor homecoming clicks; } } }
i want homecoming value of clicks clicker class, value remains whatever set in constructor within get_clicks(), , i'm not sure why.
the variable has class scope, why homecoming default value (in case, 5) of clicks get_clicks()? clicked() method traces correctly updated value. scope issue? i'm confused.
this first frame create object;
import flash.display.displayobject; import flash.events.mouseevent; import flash.text.textfield; var clicks:textfield = new textfield(); var circle:clicker = new clicker(); clicks.text = circle.get_clicks().tostring(); trace(circle.get_clicks()); addchild(circle); addchild(clicks);
as you'd expect problem i'm having, trace spits out 5 on , over, , clicks doesn't alter 5.
edit:
there mistake, fixing has caused clicks not update @ all. had library version of movieclip on first frame rather adding object frame addchild. i've added circle, clicks not update clicked() method isn't beingness triggered when click object.
resolved:
import flash.display.displayobject; import flash.events.mouseevent; import flash.text.textfield; var clicks:textfield = new textfield(); var circle:clicker = new clicker(); var mytimer:timer = new timer(100,0); mytimer.addeventlistener(timerevent.timer, timerlistener); function timerlistener (e:timerevent):void{ trace("timer triggered"); trace(circle.get_clicks()); clicks.text = circle.get_clicks().tostring(); addchild(circle); addchild(clicks); } mytimer.start();
rather utilize timeline , frames, wrapped code wanted repeat in timer listener. works intended.
actionscript-3
Comments
Post a Comment