0

I am having a lot of trouble understanding how to pass attributes around between functions in a specific way using AS3. How do I change the code below so that the deleteButton function can retrieve the variable myLike from the mySelection function?

import flash.ui.Keyboard;
import flash.events.MouseEvent;
import flash.ui.Mouse;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
btnDelete.addEventListener(MouseEvent.CLICK, deleteButton);

function keyPressed(event:KeyboardEvent)
{

    var myLike:String;

    if (event.keyCode==49) {
        myLike = "ice cream";
    }
    else if (event.keyCode==50) {
       myLike = "chocolate milk";
    }
    else if (event.keyCode==51) {
       myLike = "cookies";
    }
    else
    {
        myLike = "nothing";
    }

    mySelection(myLike);
}

function mySelection(myLike)
{
    trace("I select " + myLike + ".");
    deleteButton(myLike);
}

function deleteButton(event:MouseEvent, myLike) {
    trace(myLike + "has been deleted");
}

2 Answers 2

1

A couple of problems you're looking at are:

  1. myLike in mySelection() and myLike in deleteButton() are not the same myLike; they're two separate variables with the same name. mySelection()'s myLike can only be used inside of mySelection(), and deleteButton()'s myLike can only be used inside of deleteButton().

  2. You have another parameter inside of deleteButton(), called event, that isn't being used. Also when you try to call deleteButton() inside of mySelection(), you only pass in one argument, whereas deleteButton() has two different parameters: event and myLike.

So there are some grammatical issues essentially, and there appear to be a couple of other issues with what you're trying to do in your code, so I really need to recommend that you try to find a tutorial that does more in terms of just taking things one step at a time. You need to learn about things like scope, global variables, classes, parameters, etc.

But just as far as these two functions are concerned, in and of themselves, this should work:

function mySelection(myLike)
{
    trace("I select " + myLike + ".");
    deleteButton(myLike);
}

function deleteButton(myLike) {
    trace(myLike + "has been deleted");
}
Sign up to request clarification or add additional context in comments.

Comments

0
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
btnDelete.addEventListener(MouseEvent.CLICK, onButtonClicked);

function keyPressed( event:KeyboardEvent ):void
{
    var myLike:String;

    if (event.keyCode==49) {
        myLike = "ice cream";
    }
    else if (event.keyCode==50) {
       myLike = "chocolate milk";
    }
    else if (event.keyCode==51) {
       myLike = "cookies";
    }
    else
    {
        myLike = "nothing";
    }

    deleteButton( myLike );
}

// presuming your button instance names are the same as the names in the switch statement 
function onButtonClicked( evt:MouseEvent ):void {
    var deleteThis:String = evt.target.name;

    deleteButton( deleteThis );
}

function deleteButton( myLike:String ):void {
    trace(myLike + "has been deleted");
}

This code does not actually delete anything though. If you want to delete the button that was clicked on try this for the onButtonClicked method:

function onButtonClicked( evt:MouseEvent ):void {
    var deleteThis:String = evt.target.name;

    // path to the display list your button is in
    removeChild( evt.target );
}

Comments

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.