PDA

View Full Version : HitTest with variable names in Actionscript 3.0


Scorp-D
12-05-2007, 08:31 AM
I’m used to Java, but wanted to try Actionscript, because my teacher told me that it was an exciting language to work in.
So I made an Actionscript 2.0 game, which only took a couple of hours to make.

BUT now I’m trying to convert it into Actionscript 3.0, so far I have used a couple of days, and have found a problem that I can’t seem to solve.

Question:
I used hitTest() in Actionscript 2.0 to check if a element was above one of 25 other elements, and the underlying element should go to its own frame 2.
I have read that it might be possible to use getBounds () on a movieclip, which returns a rectangle, that can be used in containsRect(), but I haven’t found any examples that contrasts what I’m doing.

I have created the following code in Actionscript 3.0 which works until the ’gotoandstop’ call (placed in an .as file).
public function ButtonUp(event:MouseEvent):void
{
for(var i =1;i<=25 ;i++)
{
var myTargetName1:String = "element" + i;
var myTarget1: DisplayObject = getChildByName(myTargetName1);
if(event.target.dropTarget != null && event.target.dropTarget.parent == myTarget1)
{
myTarget1.gotoAndStop(2);
break;
}
}
}






This is the Actionscript 2.0 code that I’m trying to convert (code is placed in the frame of which the elements are, and ):
function checkTarget(drag)
{
for(i =0;i<=25 ;i++)
{
if(drag.hitTest(_root["element"+i]))
{
tellTarget(_root["element"+i])
{
gotoAndStop(2);
}
}
}
}

Code is executed by:

elementRemover.onPress = function()
{
checkTarget(this);
}


Scorp-D

mojito
12-05-2007, 08:43 AM
seems that you are trying to tell a string to gotoandstop this is a method of the MovieClip class since way back, you would need to check that your myTarget1 is able to have this method, though the compiler should tell you that problem ?

I just started to look at 3 myself but this seems evident ?

Nomad2000
12-05-2007, 08:47 AM
Check out the DisplayObject.hitTestObject() function. It does collision detection between two DisplayObjects (which includes Sprites and MovieClips too, since they are subclasses).

Nomad2000
12-05-2007, 08:51 AM
Hey mojito, I think you misread his code. myTarget1 is declared as a DisplayObject, not a string. myTargetName1 is a String.

Scorp-D
12-05-2007, 09:12 AM
But how does i tell the element movieclip gotoandstop(2) ?

it isent like Actionscript 2.0 ? with: (_root["element"+i]).gotoandstop(2);

mojito
12-05-2007, 09:24 AM
Yes misread and a bit of misunderstood, it must cast that string into a display type object which is presumably sprite or movieclip type ? That is the problem maybe what is this object, and check the methods available to that class.:o :eek: :( ;)

Nomad2000
12-05-2007, 09:35 AM
You can call getChildByIndex() and use the numChildren property to iterate through the children and then hitTestObject() against each of them until you find one that got hit.


var mainObj:MovieClip = ???; // Assign to whatever is the main object you are hit testing against
for (var i:int=0; i<this.numChildren; i++)
{
var obj:DisplayObject = this.getChildAt(i);
if (obj.hitTestObject(mainObj))
{
obj.gotoAndStop(2);
}
}


If you are not writing the code on the main timeline, then replace "this" above with the name of your main Sprite or MovieClip.

Scorp-D
12-05-2007, 10:40 AM
i dont get it !

My elements are in the layer elementlayer, the element that removes the other elements is named elementRemover.

i try this:

var mainObj:MovieClip = elementRemover;
for (var i:int=0; i<elementlayer.numChildren; i++)
{
var obj:DisplayObject = elementlayer.getChildAt(i);
if (obj.hitTestObject(mainObj))
{
obj.gotoAndStop(2);
}
}

and i get the error: "1061: call to possibel undefined method gotoandstop through a referance with static type flash.display:DisplayObject".

I have't made any of the elements og layers in the .as file, is it because of that ?

Nomad2000
12-06-2007, 01:18 AM
Are all your child objects MovieClips? If so, change DisplayObject to MovieClip. gotoAndStop() is a method of MovieClip.