Echoez
(?)Community Member
- Posted: Tue, 05 Feb 2013 01:36:36 +0000
This is my first time using AS3.
I'm trying to make a simple point and click game for Valentine's day, and I've already run into an issue.
Problem: Trouble calling an event raised by a specific object in an array.
Currently the event only works with the last object added on the screen.
I've tried using a for loop to assign Listeners to each object using their instance names, which I've commented out, but that only gave me back an error, so I'm assuming I'm not going about it in the right way.
Update: Apparently instance names can't be used in code.
I've changed the code a little bit, but the problem still persists. I believe the error lies in the function (last thing in the code). I've tried using "e.play()" so it will refer to the object that raised the event instead of "heart.play()", but apparently you can't do that. :c
Any advice?
Code on Pastebin
Update: [Solved]
I knew I was on a right track with "e.play()", but apparently in AS3 you have to include something extra to make it work. "e.target.play()".
I added comments to the last few lines in my code for anyone else that might have this problem in the future.
I'm trying to make a simple point and click game for Valentine's day, and I've already run into an issue.
Problem: Trouble calling an event raised by a specific object in an array.
Currently the event only works with the last object added on the screen.
I've tried using a for loop to assign Listeners to each object using their instance names, which I've commented out, but that only gave me back an error, so I'm assuming I'm not going about it in the right way.
Update: Apparently instance names can't be used in code.
I've changed the code a little bit, but the problem still persists. I believe the error lies in the function (last thing in the code). I've tried using "e.play()" so it will refer to the object that raised the event instead of "heart.play()", but apparently you can't do that. :c
Any advice?
Code on Pastebin
var heart:MovieClip;// sets a variable name for the heart movieclip
var heartArray:Array = []// makes an empty array to hold multiple hearts later
var x_:int = 0;// x and
var y_:int = 0;// y positions for the hearts
var randNum:int;// variable to hold a random number
var low:int = 50;// low and
var high:int = 150;// high range to limit the random numbers
var i:int = 0;// number used for loops
var q:int = 5;// quantity of hearts
// loop to fill the heart array
for (i = 0; i < q; i++)
{
heart = new bubbleheart()// assigns movieclip to the var name we set earlier
heartArray[i] = heart;// fills up the array with hearts
x_ += 90;// increments the x and y positions,
y_ += 75;// so the hearts won't appear at the same place
heart.x = x_;// uses the incremented x and
heart.y = y_;// y positions
// obtains a random number with our range we set earlier
randNum = Math.random()*(1 + high - low) + low;
heart.width = randNum;// uses the random number to decide the width
heart.height = randNum;// and height of the hearts
addChild(heart)// adds a heart to the screen
// stops the movieclip from playing
heart.stop()
}
// listens for a click event from the user
for (i = 0; i < q; i++)
{
heartArray[i].addEventListener(MouseEvent.CLICK,clickheart)
}
// function that performs after a click is heard/detected;
// THIS IS THE NON-WORKING VERSION
function clickheart(e:MouseEvent):void
{// plays the movieclip
heart.play()
}
// THIS IS THE WORKING VERSION
function clickheart(e:MouseEvent):void
{// plays the movieclip
e.target.play()
}
var heartArray:Array = []// makes an empty array to hold multiple hearts later
var x_:int = 0;// x and
var y_:int = 0;// y positions for the hearts
var randNum:int;// variable to hold a random number
var low:int = 50;// low and
var high:int = 150;// high range to limit the random numbers
var i:int = 0;// number used for loops
var q:int = 5;// quantity of hearts
// loop to fill the heart array
for (i = 0; i < q; i++)
{
heart = new bubbleheart()// assigns movieclip to the var name we set earlier
heartArray[i] = heart;// fills up the array with hearts
x_ += 90;// increments the x and y positions,
y_ += 75;// so the hearts won't appear at the same place
heart.x = x_;// uses the incremented x and
heart.y = y_;// y positions
// obtains a random number with our range we set earlier
randNum = Math.random()*(1 + high - low) + low;
heart.width = randNum;// uses the random number to decide the width
heart.height = randNum;// and height of the hearts
addChild(heart)// adds a heart to the screen
// stops the movieclip from playing
heart.stop()
}
// listens for a click event from the user
for (i = 0; i < q; i++)
{
heartArray[i].addEventListener(MouseEvent.CLICK,clickheart)
}
// function that performs after a click is heard/detected;
// THIS IS THE NON-WORKING VERSION
function clickheart(e:MouseEvent):void
{// plays the movieclip
heart.play()
}
// THIS IS THE WORKING VERSION
function clickheart(e:MouseEvent):void
{// plays the movieclip
e.target.play()
}
Update: [Solved]
I knew I was on a right track with "e.play()", but apparently in AS3 you have to include something extra to make it work. "e.target.play()".
I added comments to the last few lines in my code for anyone else that might have this problem in the future.