Question

I've just started to learn actionscript for a month or so, and I've been stuck on this part of the code for about 2 days now ><

So basically, I wanted to write a hitTestObject in a movieclip ("fire_mc") to detect if it overlaps the the child I added on stage (enemy)

So here's what I wrote in the main stage...

    var newtarget:enemy=new enemy();
stage.addChild(newtarget);
    newtarget.x=40;
    newtarget.y=30;

and every time I traced the newtarget in the fire_mc, it turns out to be NULL... So should I be tracing enemy or newtarget? (Cuz I tried both and nothing works). And how can I hitTestObject in the fire_mc movieclip?

Is it possible for me to hitTestObject newtarget if I were to create multiple newtarget-s? If not, what should I do?

And can someone tell me the difference between root, and MovieClip(root) (Because sometimes in a movieclip I have to use the former one to work, and sometimes the latter, and I have no idea why cuz I'm all referencing to the same place)

Sorry for these simple and lengthy questions, I'm really just a noob and I tried looking up for answers in the web for hours, but nothing helpful turned up><

Thanks in advance :)

Was it helpful?

Solution

In this statement

var newtarget:enemy=new enemy();

var - keyword used to define varibles, newtarget - variable name in which pointer to new class instance stored, :enemy - data type (the class name), new - keyword used to create new class instances, and finally enemy is class constructor (by the way there is a rule of good manners by which class names are capitalized)

So answer for you question which should you use when you want check is some is overlapping is 'newtarget'.

Now about hit test - all you need do to check if two objects hit each other is correctly use their references from the part of project where your code is writen. For example if you have your fire_mc on MainTimeline created by IDE, and code of creation of you enemy instance stored in newtarget sameplace, then if you check will be placed in frame of MainTimeline where both object present it will be like this

fire_mc.hitTestObject(newtarget);

or this newtarget.hitTestObject(fire_mc); All these statements give you same result - if objects intersect each other you have true returned.

If you created 'newtarget' on MainTimeline and checks will be from fire_mc (which is on MainTimeline to) frame, then code will something like that

this.hitTestObject(MovieClip(root).newtarget);

Now about root. Primarily it is a property of DisplayObject in which reference to the top-most display object in swf-file structure is stored. So as such it is inherited by all child classes. So here's the trick. When you try get your objects in swf structure using root here the differences are based on the fact that root always return object as DisplayObject and that means what if you object contains something that DisplayObject shouldn't you can't get it. So you must convert returned reference to need data-type usign MovieClip(...).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top