Reputation: 516
I'm new to action script 2.0, what I want is to use the local variables inside the anonymous function
var count = 0;
var evtObject = new Object();
Key.addListener(evtObject);
evtObject.onkeypress = function()
{
if(Key.UP == Key.getCode())
{
// here i want to use the count value., count++;
trace(count);
}
}
Inside the if block i want to use the count value. Even though knowing that it wont work, I used it in the anonymous function of onkeypress, it obviously showed me undefined. Kindly help me to go through this.
Upvotes: 0
Views: 203
Reputation: 187
Here i have given simple example of usage of local variable into function Please refer this Code..
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class keylister
{
var alldisplay:MovieClip;
var left:uint = 37;
var up:uint = 38;
var right:uint = 39;
var down:uint = 40;
var pickUpsArray:Array = new Array();
for (var i = 0; i < alldisplay.numChildren; i++ )
{
if(alldisplay.getChildAt(i) is littleheart)
{
pickUpsArray.push(alldisplay.getChildAt(i));
}
}
public function keylister(Display:MovieClip)
{
alldisplay = new MovieClip();
alldisplay = Display;
alldisplay.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
alldisplay.addEventListener(Event.ENTER_FRAME, PickUpItems);
}
public function keyDownListener(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
alldisplay.box_mc.x-=10;
}
if (e.keyCode == 38)
{
alldisplay.box_mc.y-=10;
}
if (e.keyCode == 39)
{
alldisplay.box_mc.x+=10;
}
if (e.keyCode == 40)
{
alldisplay.box_mc.y+=10;
}
}
public function PickUpItems(e:Event):void
{
for (var j = 0; j < pickUpsArray.length; j++ )
{
if (alldisplay.box_mc.hitTestObject(pickUpsArray[j]))
{
alldisplay.removeChild(pickUpsArray[j]);
}
}
}
}
}
Upvotes: 1