-
HitTest on multiple instances of the same MC?
I'm hoping this is a simple question, but something tells me it's not.
Currently, I'm trying to figure out how to apply the hitTest to multiple objects for a side scroller game. Right now, I have the MC:hero hitTest against the MC:ground and MC: platform.
Problem is, if I want multiple platforms (IE: copying and pasting the orginal MC: platform) he will only hitTest on the 1st instance (platform1). I've since read up on it and learned that Flash will only hitTest on that first instance. Fair enough.
But is there a way to make that MC: platform univerally hitTest my MC:man? As it is right now, I have to code into the MC:man instance each and every platform instance. It's not a big deal, but after about 5 platforms, the codes seems unnecesarily long. I'm new to AS so I know there's got to be an easier way. Maybe arrays or something?
Thank you in advance
-
var x:Number = 1; x /= 0;
You could put this in the first frame of the platform movie clip:
Code:
this.onEnterFrame=function(){
if(this.hitTest(_root.man){
//your code here...
}
}
-
Maybe I dont know what I'm doing, but I was unable to get that coding to work. Here's my actual "jump" coding for my "hero" instance (man).
if (_root.mainGround.hitTest(_x, _y, true) or _root.platform.hitTest(_x, _y, true) or _root.platform2.hitTest(_x, _y, true)) {
grounded = true;
jumpcount = 0;
jumpSpeed = 20;
} else {
airborne = true;
_y += gravity;
state = 3;
}
if (Key.isDown(Key.SPACE)) {
_y -= jumpSpeed;
jumpcount += 1;
state = 3;
}
if (jumpcount>maxjump) {
jumpSpeed -= 1;
}
Perhaps I should have just posted this in the beginning. But as you can see the numbering on platforms makes that first line start to get unruly real fast
I couldn't really see how to apply this to the code you posted. I tried taking out the
if (_root.mainGround.hitTest(_x, _y, true) or _root.platform.hitTest(_x, _y, true) or _root.platform2.hitTest(_x, _y, true)) {
grounded = true;
jumpcount = 0;
jumpSpeed = 20;
} else {
airborne = true;
_y += gravity;
state = 3;
}
part outta the hero instance and then adapting it with your code in the platform frame like you said. Unfortunately, and again, I may have done it wrong, but now the platform takes the properties of the hero. IE: when I walk away from the platforms, the gravity variable takes hold and the platforms shoot downwards.
-
var x:Number = 1; x /= 0;
The reason your platforms are now reacting as the hero should is that you have to change your code a little bit so that it is specifically referencing the hero.
in the platform mc you would want something like this:
Code:
this.onEnterFrame=function(){
if(this.hitTest(_root.man._x,_root.man._y,true)){
_root.man.grounded=true;
}
}
in the hero you would then want something like this:
Code:
if(grounded){
jumpcount=0;
jumpSpeed=20;
}else{
airborne=true;
_y+=gravity;
state=3;
}
See if that helps.
-
Senior Member
Having many or multiple onEnterFrames() can get a bit messy..and a resource hog..
I havent read everything thoroughly....
but but maybe put all the 'platform' instance names in an array..
(either static or dynamically populating/editing the array depending on the platforms on the stage)
and looping through the array and checking hitTest() on each index (platform instance)..
executing this 'function' based on whatever action the guy takes and when you need to check for the hittest().
like while jumping == true; for example..
-
it's getting closer, Zippy, but now, when I put the codes in, and press SPACE, the hero jumps and continues up so long as I hold the SPACE bar. When I let go, he just stays there, doesn't fall anymore. I've been fighting with code trying to get gravity used again, but it seems like its not registering the hitTest in the platform MC and getting into that "grounded" state
-
var x:Number = 1; x /= 0;
You just need to make sure you set airborne to true when you start to jump.
Code:
if (Key.isDown(Key.SPACE)) {
if(jumpcount==0) airborne=true;
_y -= jumpSpeed;
jumpcount += 1;
state = 3;
}
if (jumpcount>maxjump) {
jumpSpeed -= 1;
}
-
then dont forget to put airborne = false; when it is grounded
-
So I've been trying multiple ideas to this and then starting to play around with Whispers idea (the arrays)
I think I'm getting it, but...
Actionscript Code:
onClipEvent (load) { gravity = 10; moveSpeed = 4; maxjump = 6; _root.platforms = new Array("platform1", "platform2", "platform3"); }
onClipEvent (enterFrame) { for (var i = 0; i<_root.platforms.length; i++) { if(this.hitTest(_root[_root.platforms[i]])){ grounded = true; jumpcount = 0; jumpSpeed = 20; trace ("CONTACT"); } else { airborne = true; _y += gravity; state = 3; trace ("---") } } if (Key.isDown(Key.SPACE)) { _y -= jumpSpeed; jumpcount += 1; state = 3; } if (jumpcount>maxjump) { jumpSpeed -= 1; } }
Currently, he just falls. I put the trace line in to give me an idea of what is going on...and I get a readout like
---
---
CONTACT
---
---
CONTACT
Leading me to believe that my loop is constantly checking all three platforms and not stopping when the hero contacts any platform. I mean, there's a quick register when he contacts, say Platform1, but then the code runs to platform2, which he isn't, then starts falling again.
Any way to stop that loop with the IF statement is true?
-
var x:Number = 1; x /= 0;
Code:
if(this.hitTest(_root[_root.platforms[i]])){
grounded = true;
airborne=false;
jumpcount = 0;
jumpSpeed = 20;
trace ("CONTACT");
}
-
Iit still doesn't work. This is kinda my error actually.
The lines
grounded = true
airborne = false
are leftovers from a broken code. They aren't attached to anything or variable and can just as easily be taken out of the code. I just kept them in for references. As such, changing things to true and false doesn't change anything.
Maybe my initial code thinking is wrong. I have it set up to essentially ALWAY apply gravity to the hero. Gravity stops with a hitTest on the ground.
Perhaps I should work the jumping aspect differently?
-
var x:Number = 1; x /= 0;
Ahh, I think I found it!
Instead of this:
Actionscript Code:
for (var i = 0; i<_root.platforms.length; i++) { if(this.hitTest(_root[_root.platforms[i]])){ grounded = true; jumpcount = 0; jumpSpeed = 20; trace ("CONTACT"); } else { airborne = true; _y += gravity; state = 3; trace ("---") } }
try this:
Actionscript Code:
grounded=false; for (var i = 0; i<_root.platforms.length; i++) { if(this.hitTest(_root[_root.platforms[i]])){ grounded = true; jumpcount = 0; jumpSpeed = 20; trace ("CONTACT"); } } if(!grounded){ _y += gravity state = 3; trace ("---") }
-
There it is!!
THANK YOU VERY MUCH. Been fighting with that line of code for about 3 days now!!....
Again, thx for all the help!
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
|