0

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.

1
  • "I'm new to action script 2.0" start investing time with ActionScript 3.0 and also it will prepare you for other C-like languages (eg: Java, C#, Swift etc)... Anyway : What is if(Key.UP == Key.getCode()) trying to achieve? Key.UP is when a key stops being pressed (released) so what Key.getCode() could it be ever be Equal To? Are you looking for the "up" arrow key? Commented May 31, 2017 at 13:06

1 Answer 1

1

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]);
            }
        }
    }   


    }

   } 
Sign up to request clarification or add additional context in comments.

1 Comment

This looks like AS3 code. Asker is using AS2. Also things like is littleheart are going to cause errors like "Undefined..." when they test. Best to fix Asker's own provided example code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.