-
Accessing variable in custom class gives 'Undefined'
Hi
I am trying to access a variable inside my custom class through a mouse event in my main .fla file but I keep getting undefined.
Card.as Custom Class
Code:
package {
// Import all required files
import flash.display.MovieClip;
public class Card extends MovieClip {
// Variables
public var index:int;
// Constructor
function Card(i:int):void{
//trace("New Card Made");
this.index = i;
}
}
}
Main .fla File
Code:
// Set up container
var puzzelArea:Sprite = new Sprite();
puzzelArea.x = 230;
puzzelArea.y = 100;
addChild(puzzelArea);
// Start game when button is pressed
function startPuzzel(evt:MouseEvent):void {
trace("Button pressed");
drawCard();
}
bPlay.addEventListener(MouseEvent.CLICK, startPuzzel);
// Function used to draw a new card
function drawCard():void{
var newCard:Card = new Card(5);
puzzelArea.addChild(newCard);
newCard.addEventListener(MouseEvent.CLICK, clickCard);
trace("New card index in drawCard function: " + newCard.index); // This works
}
// Function used to click on a card
function clickCard(evt:MouseEvent):void{
trace("Card clicked");
trace("New card index in clickCard function: " + evt.target.index); // This gives undefined
}
When I run the .fla file I can draw the card and click on it but I in the output window I get:
Code:
Button pressed
New card index in drawCard function: 5
Card clicked
New card index in clickCard function: undefined
Thanks
-
Senior Member
I tested your script and it works fine. I get 5 in both cases. I can only imagine that in your case you have bubbling and target refers to a different object. Check what you get with trace(evt.target). It is always better to use currentTarget if you are referring to the actual target.
- The right of the People to create Flash movies shall not be infringed. -
-
Thanks for your reply. Based on the code given I cant see how I may be referring to another object, also what does the term bubbling mean.
-
Senior Member
"Bubbling" means that above the immediate target there are other targets. In your case there is puzzelArea and of course the timeline as targets on top newCard. Take a look at this:
http://www.flashscript.biz/flashas3/..._bubbling.html
Use currentTarget, which will always refer to the closest target to the eventlistener.
- The right of the People to create Flash movies shall not be infringed. -
-
Hi
Thanks for your reply. I looked into 'Bubbling' but this was not the case as I tried by directly adding to stage via 'addChild(newCard)'.
However, upon using 'evt.currentTarget.index' as opposed to 'evt.target.index' I was able to access all variable properties of the custom class when I wanted.
Thanks a lot for your help. This has really been problematic for me but now I am able to get on with things.
-
Senior Member
Since currentTarget worked you have experienced bubbling, since by using target you got a different target from what you wanted, higher in the list.
- The right of the People to create Flash movies shall not be infringed. -
-
When I used 'evt.target' as opposed to 'evt.currentTarget' when attempting to access the index property in the custom class, I was given 'undefined' even though the object had been added to the stage without any containers however upon using 'evt.currentTarget' I was able to access the index property regardless whether the object was inside a container or not. I have had this issue for a while now but I now understand why this occurs.
I have learned something new here (bubbling) on my first post which is always a plus so if I do encounter anything that I am having problems with I know where to ask.
Once again thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|