문제

I want to display item names when the mouse is over them. And item name variable is in item class. I have an object called apple on the scene. I named it "Apple" in the code. When I call for itemName in a function on the same code, it says it's not defined when I mouseover the object. If I trace the apple.itemName outside the function, it works. I don't know why. This is my code:

The code on my scene:

import flash.events.MouseEvent;
import inventory.inventorySystem;
import inventory.item;

var IS:inventorySystem;
var IT:item;

apple.itemName = "Apple";
apple.itemIcon = new AppleIcon();
apple.addEventListener(MouseEvent.MOUSE_OVER, showItemNameF);

function showItemNameF(Event:MouseEvent){
    var itemNameBox:TextField;
    itemNameBox.text = this.itemName;
    itemNameBox.x = mouseX;
    itemNameBox.y = mouseY;
}

The item class:

package inventory {

    import flash.display.MovieClip;

    public class item extends MovieClip{

            public var itemName:String;
        public var itemIcon:MovieClip;
    }
}
도움이 되었습니까?

해결책

this in your event handler function does not refer to your apple instance. (Are you coming from AS2 maybe?) this is the reference to your class/timeline where your listener code is located and which does not have any itemName variable defined. To be able to retrieve the name you would have to do something like:

Apple(e.currentTarget).itemName //assuming that Apple is your class of the apple instance.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top