View Full Version : Targeting an object created in a for loop
usualnoodle
11-08-2007, 02:14 PM
Hi all,
Please can you help? I am trying to create new sprites inside a for loop, I then want the sprite that I hover over to get bigger. At the moment all the sprites are scaling together and not just the one I hover over.
Im guessing it is because I am not giving each Sprite an different variable name but Im not sure how to do this inside a for loop. Does anyone know the solution?
Heres the code:
package com.edition.slideshow
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class SlideShowTest extends Sprite
{
public function SlideShowTest ()
{
for (var i:int = 0; i < 2; i++)
{
var hoverIndicator:Sprite = new Sprite;
hoverIndicator.graphics.beginFill(0xFF0000, 0.5)
hoverIndicator.graphics.drawRect(0,0,30,20)
hoverIndicator.graphics.endFill()
addChild(hoverIndicator)
hoverIndicator.x = 200 * i
hoverIndicator.addEventListener(MouseEvent.MOUSE_O VER, hoverHandler);
}
}
private function hoverHandler(event:MouseEvent):void {
this.scaleX = 2
}
}
}
:confused:
FrodoBaggins
11-08-2007, 02:40 PM
use event.target.scaleX
panel
11-08-2007, 02:59 PM
or
event.currentTarget.scaleX
usualnoodle
11-08-2007, 03:14 PM
Thank you, that worked but I want these sprites to be buttons that take you to somewhere else. How would I pass a variable from say "button 2" to then do the button two function or go to the button two page?
gggmork
11-08-2007, 03:59 PM
I'm a newbie but think I might have figured out how to make each a seperate variable (also I think the 'this' you used refers to the entire display area?):
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class SlideShowTest extends Sprite
{
var hoverIndicator1 = new Sprite
var hoverIndicator2 = new Sprite
var list:Array = new Array(2)
var i = 0
public function SlideShowTest ()
{
list[0] = hoverIndicator1
list[1] = hoverIndicator2
for (i; i < 2; i++)
{
list[i].graphics.beginFill(0xFF0000, 0.5)
list[i].graphics.drawRect(0,0,30,20)
list[i].graphics.endFill()
addChild(list[i])
list[i].x = 200 * i
list[i].addEventListener(MouseEvent.MOUSE_OVER, hoverHandler);
}
}
private function hoverHandler(event:MouseEvent):void
{
event.currentTarget.scaleX = 2
}
}
}
usualnoodle
11-08-2007, 04:30 PM
Thanks for the reply but I cant seem to make it work for an indefinite loop and Im having problems setChildIndex to front with the eventHandler. Here is the code:
package com.edition.slideshow
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class SlideShowTest extends Sprite
{
var list:Array = new Array()
public function SlideShowTest ()
{
for (var i:int = 0; i < 10; i++)
{
var list[i]:Sprite = new Sprite;
list[i].graphics.beginFill(0xFF0000, 0.5)
list[i].graphics.drawRect(0,0,30,20)
list[i].graphics.endFill()
addChild(list[i])
list[i].x = 100 * i
list[i].addEventListener(MouseEvent.MOUSE_OVER, hoverHandler);
list[i].addEventListener(MouseEvent.CLICK, clickHandler);
}
}
private function hoverHandler(event:MouseEvent):void {
event.target.scaleX = 1.2
event.target.scaleY = 1.2
setChildIndex(Sprite(event.currentTarget),numChild ren-1);
}
private function clickHandler(event:MouseEvent):void {
trace(event.currentTarget.name)
}
}
}
I get a Error #1086 for this line
var list[i]:Sprite = new Sprite;
Also get a error #2025 for trying to change the depth index
setChildIndex(Sprite(event.currentTarget),numChild ren-1);
Does anybody know how to sort these problems???
:eek:
gggmork
11-08-2007, 08:10 PM
It seems you're trying to create and asign variables automatically in a function. I actually had this same question for the python programming language once and someone on a forum had an answer.
I'm pretty sure this is how to automate variable creation IN PYTHON:
-----------
for i in range(100):
exec("count%d = " + str(i)) % i
print count0, count1, count2, count3... (etc. to count100)
-----------
All those count variables should have been created without me tediously having to create them. But I don't know what the equivalent for this is in actionscript. I think the exec command interprets a string as something to immediately execute or something. It might be fine to just create each variable and put them in an array first. Also apparently eval and exec commands have security risks because they can instantly run malicious code or something. I have no idea what I'm writing about really.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.