ed Mack ... that code you posted on the front page was EXACTLY!!!! What I've been trying to write all day long! I LOVE YOU! HEHEHEHEH!
Printable View
ed Mack ... that code you posted on the front page was EXACTLY!!!! What I've been trying to write all day long! I LOVE YOU! HEHEHEHEH!
I'm building an art based scrolling rpg and have had 2 versions of code so far.
I can scroll and collide with the perimiter ala shape flag but extra collisions with other mcs is really giving me a headache. The hit doesn't work and the sound just loops and loops, I tried onLoad function but this played sound once and hit test didn't work :*(
please can somebody take a look and see if they can solve this for me....
Code type 1 is:-
//on hero mc
onClipEvent (load) {
end=true;
move = 0;
lock = true;
}
onClipEvent (enterFrame) {
if (!_root.universe.world.hitTest(this._x-xstep, this._y-ystep, true)) {
_parent.universe._x += Xstep;
_parent.universe._y += Ystep;
Ystep = 0;
Xstep = 0;
hit=false
} else{hit=true;}
if (Key.isDown(Key.UP)) {
Ystep = 10;
Xstep = 0;
this.gotoAndStop("back");
} else if (Key.isDown(Key.DOWN)) {
Ystep = -10;
Xstep = 0;
this.gotoAndStop("front");
} else if (Key.isDown(Key.LEFT)) {
Xstep = 10;
Ystep = 0;
this.gotoAndStop("left");
} else if (Key.isDown(Key.RIGHT)) {
Xstep = -10;
Ystep = 0;
this.gotoAndStop("right");
}
if(xstep or ystep ){;hero.play();}
else{hero.gotoAndStop(1)}
if(hit){hero.gotoAndStop(1)}
//the bit I'm stuck on which on house1 collision plays yell frame 2 to play sound
onClipEvent (enterFrame) {
if (_root.hero.hitTest (_root.stage.house1) == true)
{
speed = 0;
_root.yell; gotoAndPlay ("3")
}
}
and code type 2:-
//on hero mc
onClipEvent (enterFrame) {
if (_root.hero.hitTest (_root.stage.bound1))
{
speed = 0;
_root.stage._y -=5
}
else if (_root.hero.hitTest (_root.stage.bound2))
{
speed = 0;
_root.stage._x -=5
}
else if (_root.hero.hitTest (_root.stage.bound3))
{
speed = 0;
_root.stage._y +=5
}
else if (_root.hero.hitTest (_root.stage.bound4))
{
speed = 0;
_root.stage._x +=5
}
else
{
}
if (Key.isDown(Key.RIGHT) == true){
GOTOANDSTOP (3);
}
else if (Key.isDown(Key.UP) == true){
GOTOANDSTOP (2);
}
else if (Key.isDown(Key.DOWN) == true){
GOTOANDSTOP (4);
}
else if (Key.isDown(Key.LEFT) == true){
GOTOANDSTOP (5);
}
else if (Key.isDown(Key.UP) == false){
GOTOANDSTOP (1);
}
}
//bit i'm stuck on
onClipEvent (enterFrame) {
if (_root.hero.hitTest (_root.stage.house1) == true)
{
speed = 0;
_root.yell; gotoAndPlay ("3")
}
}
AND on stage aka background code type 2...
//movement
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT) == true){
_x = _x - 5;
}
if (Key.isDown(Key.LEFT) == true){
_x = _x + 5;
}
if (Key.isDown(Key.DOWN) == true){
_y = _y - 5;
}
if (Key.isDown(Key.UP) == true){
_y = _y + 5;
}
}
----------------------------------------
What I really want to do is when hero mc collides with house I want to move to stage 2 i.e go inside a building. This is really buggin me though and it was hard enough figuring out 1 code type only to scrape another together only to have the self same problem.
Please could somebody put me out of my misery??
Thanks,
Gunny.
this has prooved too complex for me.
I'd like to understand at least one of the firs codes posted and I just ask any1 with a teacher's soul and a lot of patience, to explain it to me :D
the code is this
thanks, I really appreciate it... and if you dont want to explain, at least tell my how to adapt the code so that instead of moving the character, the map scrollsCode:onClipEvent(load){
s = 5;
b = this.getBounds(this);
function move(x,y){
if(!_root.hit.hitTest(_x+x+b.xmin+1, _y+y+b.ymin+1, true)){
if(!_root.hit.hitTest(_x+x+b.xmax-1, _y+y+b.ymin+1, true)){
if(!_root.hit.hitTest(_x+x+b.xmin+1, _y+y+b.ymax-1, true)){
if(!_root.hit.hitTest(_x+x+b.xmax-1, _y+y+b.ymax-1, true)){
_x += x;
_y += y;
}
}
}
}
}
}
onClipEvent(enterFrame){
if(Key.isDown(Key.UP)){
move(0,-s);
}
if(Key.isDown(Key.DOWN)){
move(0,s);
}
if(Key.isDown(Key.LEFT)){
move(-s,0);
}
if(Key.isDown(Key.RIGHT)){
move(s,0);
}
}
I make a paint engine, It always dublicates the same instance. Now by a push of a button the all start to fall how do I create interation(collision, hitTest) between each iduvidual dublicated instance??
This is the code I have for the Instance sofahr. Do I need to use an Array for this?
PHP Code:onClipEvent(enterFrame) {
if(keyPressed == true) {
if(i<10){
i++;
if (this.hitTest(_root.Vice_Brick+i)){
hit = true;
fall = true;
}
}
}
}
If you apply this to a box...
This establishes the speed and boundriesCode:onClipEvent(load){
s = 5;
b = this.getBounds(this);
top-right corner of the MCCode:
function move(x,y){
if(!_root.hit.hitTest(_x+x+b.xmin+1, _y+y+b.ymin+1, true)){
Bottom right corner of the MCCode:if(!_root.hit.hitTest(_x+x+b.xmax-1, _y+y+b.ymin+1, true)){
Top Left corner of the MCCode:if(!_root.hit.hitTest(_x+x+b.xmin+1, _y+y+b.ymax-1, true)){
Bottom left corner of the MCCode:if(!_root.hit.hitTest(_x+x+b.xmax-1, _y+y+b.ymax-1, true)){
that's the coding to make the "Hero" move.Code:onClipEvent(enterFrame){
if(Key.isDown(Key.UP)){
move(0,-s);
}
if(Key.isDown(Key.DOWN)){
move(0,s);
}
if(Key.isDown(Key.LEFT)){
move(-s,0);
}
if(Key.isDown(Key.RIGHT)){
move(s,0);
}
}
______________________________________________
MY QUESTION:
Is it possible to do a hittest through different MCs? I have an MC of my hero swinging a fish, in which a hit area comes up, and if a target inside a different MC is within the hit area of the hero MC, I want the MC of the target to go to frame 2...
And is it possible to have 2 different hitTests inside the same MC?
Does anyone know some simple coding that they are SURE will work for a maze-type game so that my hero wont keep walking through the maze walls...Please if you have a simple script for Flash 5 POST IT!!! I want to be able to make my own game with the script without screwing up...
see page one
I am trying to make a birds eye game where a little guy walks around a world that I am creating slowly. Maybe it will become multiplayer (that would be cool) if my friend/server allows me to host it from the site.
Anyways, There are rocks on the gorund and I want the little man to stop when her runs into one. I have figured out how to make him move, here is what I have:
onClipEvent (enterFrame) {
if (Key.isDown(key.Right)) {
this._x += 3;
this.gotoAndStop("WalkEast");
} else if (Key.isDown(key.Down)) {
this._y += 3;
this.gotoAndStop("WalkSouth");
} else if (Key.isDown(key.Left)) {
this._x -= 3;
this.gotoAndStop("WalkWest");
} else if (Key.isDown(key.Up)) {
this._y -= 3;
this.gotoAndStop("WalkNorth");
} else if (Key.isDown(key.Right) and key.isDown(66)) {
this._x += 6;
this.gotoAndStop("RunEast");
} else if (Key.isDown(65)) {
this._x += 0;
this.gotoAndStop("PickW");
} else {
this._y += 0;
this.gotoAndStop("StandS");
if (this.hitTest(BigRock)) {
this._y += 0;
this._x += 0;
this.gotoAndStop("StandS");
}
}
}
Note, the "else.if(key.isDown(key.Right) and key.isdown(b))" doesnt work If you know how to make it do something while pressing 2 keys at once plz help as well.
Anyways, my problem is the last IF funtion in that thread, it doesn't seem to want to work. I'll attach a zip to this forum if anyone needs me to, but later.:doughnut: :cow:
here is just a sample with 3 rocks and a little dude.
See page one, once again (Ed's post)
i wonder, why the the hittest always freeze when hit the wall/boundry?
http://darksquall.digitalrice.com/dungeon2.zip
key:
arrow to move
tab to take the blackbox, while on it
red area, the area to walk
I tried the thing on page one but nothing happened.
Hey everyone,
I was wondering the same thing that Thor mentioned a few posts before this one on July 13th. Ed Mack's hitTest Collision method only works for two MCs that are on the main stage. This causes a problem for me, since my "hero" and "hit" are within MCs.
When looking at Ed Mack's code:Code:_root.world.holder.hero; // Hero
_root.world.holder.wall; // the Wall the hero cannot pass through
I don't see a thing that would not permit it to work within any MC. I have tried using his code with my scenario (changing _root.hit to _root.world.holder.wall, for example), but the hero always passes through the wall, meaning something's just not registering in the hitTest department. The only thing that may be holding back its functionality is the use of xmax, xmin, ymin, and ymax, but I'm uncertain of that as well.Code:onClipEvent(load){
s = 5;
b = this.getBounds(this);
function move(x,y){
if(!_root.hit.hitTest(_x+x+b.xmin+1, _y+y+b.ymin+1, true)){
if(!_root.hit.hitTest(_x+x+b.xmax-1, _y+y+b.ymin+1, true)){
if(!_root.hit.hitTest(_x+x+b.xmin+1, _y+y+b.ymax-1, true)){
if(!_root.hit.hitTest(_x+x+b.xmax-1, _y+y+b.ymax-1, true)){
_x += x;
_y += y;
}
}
}
}
}
}
onClipEvent(enterFrame){
if(Key.isDown(Key.UP)){
move(0,-s);
}
if(Key.isDown(Key.DOWN)){
move(0,s);
}
if(Key.isDown(Key.LEFT)){
move(-s,0);
}
if(Key.isDown(Key.RIGHT)){
move(s,0);
}
}
If any of you can figure out a way to get around this problem, I'm sure many people would be grateful. I, for one, thank you in advance for any help you may provide.
Thanks!
- Bean
hi bean,
I've tried ed's and work on mine, you can see it in dongeon.zip, which i've sent be4.
the only problem now is my hero stuck / freeze ( like there is an ice trap outside the red area ^^) cant go back after hit the wall
use arrow key to move and tab to take the black box.
BTW my code is spooky so prepare your eye :p :p
I made some minor change to ed's code, so the map moving, instead the hero.
BUT i found that the script, which has no error, run not well
it still walk pass the wall for some pixel, and then stuck
it should be working , shouldn't it ??
since this is a discussion about hittest i thought this was the best place to post this:
if(_root.Symbol 16, hittest(_root.Symbol 4)){
_root.score +=1;
}
whenever i try to test the scene the output says:
')' expected
and i can't figure out where it wants me to put a ')'
help:confused:
well, that usually happens to me when I use If(Key.isDown)
Because I end up typing
Instead ofCode:if(Key.isDown(Key.Right)
Not sure if thats ur problem though.Code:if(Key.isDown(Key.Right))
no, i made sure i had enough but it wants more and i don't know where.
Maybe you could try giving Symbol 16 and Symbol 4 unique instance names. Just select the individual symbol, then in the properties window, under "Instance Name", give the symbol a name that kind of describes what it is. Then use this:
Assuming you change symbol 16 to "sixteen"
and assuming you change symbol 4 to "four"
Of course you shouldn't neccesarily use "sixteen" and "four". I just used those so they resemble their original counterparts.Code:if (_root.sixteen.hitTest("_root.four")){
_root.score += 1;
}
- Bean
thx that worked i dont know how, i had their instance names the same as their symbol names.
It seems to me that your code
only had one error, and that was that you did not capatalize the word Test in hitTest. I know, it really shouldn't make a difference, but it's a Flash function, and everything is case sensitive. I'm glad it worked out, though, and you're welcome. :DCode:if(_root.Symbol 16, hittest(_root.Symbol 4)){
_root.score +=1;
}
- Bean
ok, i'll remember that.
i got another question, i'm making a basketball game and when the ball goes up it crosses the "sixteen" and adds a score to my scoreboard, how do i stop this?(ball="four")
oh and what does a case sensitive have to do with a ')'
"when the ball goes up it crosses the "sixteen" and adds a score to my scoreboard, how do i stop this?(ball="four")"
Isn't this the effect you wanted to create in the first place? Perhaps I'm not understanding your question correctly.
I guess to stop it, delete the code that I wrote for you :confused:
"oh and what does a case sensitive have to do with a ')'"
It has nothing to do with it ^_^. Your parentheses, that being '(' and ')', were used perfectly. I said the problem was your capatalization of the word "Test" in "hitTest". I referred to it being "case sensitive", which means that there's a difference between "t" and "T". I hope this clears things up.
- Bean
i mean i want the score to change when it goes through the basket(down) not when it is in the air(up)
Why not just make "sixteen" small enough so it's only on top of the basket, thus disallowing the ball to ever hit it while going upwards?
If the ball happens to go at an angle where it will "make contact with" the basket while going up, then you might have a little work to do. You could make a variable that makes sure it first went upwards, like so.
Basically, this checks if the ball is hitting "sixteen". If so, then it checks if the ball went up, which is the variable ballWentUp. The first time it checks, it wont be true, so it passes the scoring part and adds 1 to ballWentUp, which means it's true. The next time the ball hits the basket, ballWentUp will be true, so it will go into that second if statement, which adds the score, and also makes ballWentUp equal to -1 because it will then be incremented to 0, which will put it back in place for the next time that you shoot.Code:if(_root.Symbol 16, hittest(_root.Symbol 4)){
if(ballWentUp == 1){
_root.score += 1;
ballWentUp = -1;
}
ballWentUp += 1;
}
NOTE: This will only work if the ball ALWAYS hits "sixteen" while going upwards.
Hope this works out for ya!
- Bean
thx that worked perfectly
Glad to hear it finally worked out! :D
For other matters on your game, it's suggested you post a new thread so your questions aren't hidden elsewhere, such as the end of this almost four-thousand post sticky thread. ^^
You're welcome.
- Bean
this is what i have for my side scrolling platform game:
onClipEvent (keyUp){
_root.walk.gotoAndStop(1);
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)&& this._x>50) {
_root.walk.play();
_root.walk._x -= 15;
_root.walk._xscale=-100
}
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)&& this._x<495) {
_root.walk.play();
_root.walk._x += 15;
_root.walk._xscale = 100;
}
}onClipEvent (enterFrame) {
if (Key.isDown(Key.SPACE) && !jumping) {
_root.walk.gotoAndPlay(6)
vel_y = 36;
jumping = true;
}
if (jumping == true) {
vel_y -= 2;
if (vel_y<=-15) {
vel_y = -15;
}
this._y -= vel_y;
}
if (_root.ground.hitTest(this._x+30, this._y+30, true)) {
vel_y = 0;
jumping = false;
}
}
onClipEvent (enterFrame) {
this._y += 12;
if (_root.ground.hitTest(this._x, this._y+30, true)) {
this._y -= 12;
}
}
but i cant figure out how to get my character to go to the next scene when he exits off the right or left side of the screen, can anyone help? i would really appreciate it
Optimizing collision detection discussion and examples
That thread is from a while back, just forgot to put it in the stickies until now :).
hey, i'm a joinin this hitTest party,i got me a question that i can't seem to find the answer to anywhere else in this thread or elsewhere on the board. If you've read my other two posts pleading for help, you'll know that i'm putting together a zelda-style interface for my cv (i'm an animator by trade, just wanted to do something a little different :)). I eventually got my lil guy walking properly, then i managed to make him stop at objects, however, he aint stopping at everything, he'll walk right on through somethings sometimes, and the big ol' gallery at the top, he walks through that every time. As far as my feeble brain can tell, this may be because the hitTest area is partly offscreen? would this make a difference, how can i solve it? sorry for the long message but i'm well stuck! here's the swf: gruffcv , and i'll upload the fla
god, theres too much to read for a tired mind, so ill ask and hope u havent answered already, and if u have, sorry. my problem is that i want to hittest 2 things which are in an mc. should that really be a problem, cuz i just cant get it to work. ive allready sked in the object thread, but since its now a hittest problem i ask here. if you know y it wont work, please tell me.
*edit*
I just read this and i see that i should explain it a bit better. its for my mario kart racing game (or ihoss-cart, obvioulsy), and i have a map in the corner wiht the car as an icon. when i move forward the car in the map moves forward. what i want to is to see if the car is on the road and if it isnt, slow it down. therefore i added a road shape to the map and hittest it from the car icon to see if they hit.
code:
if (_parent.shape.hitTest(this._x,this._y,true)) {
_parent._parent.maxspeed=20;
}else{
_parent._parent.maxspeed=10;
}
it is relative cuz im going to load it inot another movie, and im sure all the mc exist.
i would really apreaciate it if someome could help me.
Hi Ed //
I really appreciate looking at your source files. They have been most helpful to me. I noticed a minor problem with {simple_shooter.fla} from your download page. The counter is not working after you kill an enemy. I took the second frame out in the enemy_mc because it had a stop action on that frame. That seemed to fix the problem. I know this is minor, I just thought I would bring it to your attention.
Thanks again!
:b
In scrolling, the hitTest is pretty much like moving accept backwardsQuote:
Now, "hyrossia" is the map mc. I have the map scrolling the way I want it now....but the problem now is the collision detection is out of whack (if the map is scrolling while you hit a wall you begin to go through it). Any solutions for this?
so you would have to create a sperate scroll function like
function move(x, y) {
if(!_root.map.hitTest(_x+x+b.xmin, _y+y+b.ymin, true)) {
_x += x;
_y += y;
}
function scroll(x, y) {
if(!_root.map.hitTest(_x-x-b.xmin, _y-y-b.ymin, true)) {
_root.map._x += x;
_root.map._y += y;
}
Hey,
Ive used hittest to deplete the health of my player whenever he hits a baddie movieclip - The problem is - I want the baddie to die and the player to lose say 25 of their health all in one go.
At the moment the player keeps losing life as long as he is in contact with the baddie movieclip......get me?
The way I would like it is in the bombs away game :) ie: when the plane hits the surface to air missiles they explode and the plane loses some energy....
Pete M
Hello people, i'm wondering if someone can help me out. :confused:
I've been using the following code to control a movieclip's movement with the keys. This being Ed Mack's code from the first page i think. However, what i'd really like to be able to do is to have the movieclip attached to the mouse and try and get the hit test to work that way. I've had a couple of attempts and have searched for relevant threads, but as yet i've had no luck on either count.
If anyone has any ideas or knows how to do this, i'd be really grateful for their advice.
Thanks again :D
--- code starts ---
onClipEvent(load){
s = 5;
b = this.getBounds(this);
function move(x,y){
if(!_root.hit.hitTest(_x+x+b.xmin+1,_y+y+b.ymin+1, true)){
if(!_root.hit.hitTest(_x+x+b.xmax-1,_y+y+b.ymin+1,true)){
if(!_root.hit.hitTest(_x+x+b.xmin+1,_y+y+b.ymax-1,true)){
if(!_root.hit.hitTest(_x+x+b.xmax-1,_y+y+b.ymax-1,true)){
_x += x;
_y += y;
}
}
}
}
}
}
onClipEvent(enterFrame){
if(Key.isDown(Key.UP)){
move(0,-s);
}
if(Key.isDown(Key.DOWN)){
move(0,s);
}
if(Key.isDown(Key.LEFT)){
move(-s,0);
}
if(Key.isDown(Key.RIGHT)){
move(s,0);
}
}
--- code ends ---
Do you want movie clip to move to the direction of mouse coordinates? Only when mouse is clicked? Or do you want movie clip to appear in mouse position?Quote:
Originally posted by triple j
I've been using the following code to control a movieclip's movement with the keys. This being Ed Mack's code from the first page i think. However, what i'd really like to be able to do is to have the movieclip attached to the mouse and try and get the hit test to work that way. I've had a couple of attempts and have searched for relevant threads, but as yet i've had no luck on either count.
Its not hard to change the code to do following:
*when mouse is clicked check which way mouse is from mc current position
*start moving in that direction
*move until hit the wall or mouse position is reached
Thanks for the reply Tony :)
The movieclip will be in the mouse's position, so a start drag will be fine for that or code saying the the x and y values of the movieclip equal the x and y values of the mouse, whichever would be easiest to implement into the hittest.
I tried re-working the code and included a start drag with the movieclip locked to the mouse center, but i wasn't sure how to include the move function into it. As soon as i included the start drag the hittest stopped working. The answer is probably staring me in the face, but i can't seem to find the solution just yet.
Any help you can offer would be great :D
I dont think you can use drag in this case. Try this:
code:
onClipEvent (enterFrame) {
if (_root._ymouse<this._y) {
move(0, -s);
}
if (_root._ymouse>this._y) {
move(0, s);
}
if (_root._xmouse<this._x) {
move(-s, 0);
}
if (_root._xmouse>this._x) {
move(s, 0);
}
}
That's excellent Tony, thank you very much for your help :)
It's a shame you can't use drag for this as it would have been alot smoother. Would it be possible to use the constrain to rectangle option in the start drag to limit its movement on the hittest ? For example, if the right hand side of the movie clip was hitting something, i would only have start drag for up, down and left ?? I'm not sure if it could work like that.
Once again thank you so much for your time and knowledge, it's been a big help :cool:
I never tried anything like that. First, I think you can only change constrain to rectangle option when you start the drag, that would mean you have to constantly stop dragging and start again.Quote:
Originally posted by triple j
Would it be possible to use the constrain to rectangle option in the start drag to limit its movement on the hittest ? For example, if the right hand side of the movie clip was hitting something, i would only have start drag for up, down and left ?? I'm not sure if it could work like that.
And you cant actually hittest dragged movie clip against where it would go.
If you want, you could still use the _xmouse/_ymouse like this:
code:
onClipEvent (load) {
b = this.getBounds(this);
function move (x, y) {
if (!_root.hit.hitTest(_root._xmouse+b.xmin+1, _root._ymouse+b.ymin+1, true)) {
if (!_root.hit.hitTest(_root._xmouse+b.xmax-1, _root._ymouse+b.ymin+1, true)) {
if (!_root.hit.hitTest(_root._xmouse+b.xmin+1, _root._ymouse+b.ymax-1, true)) {
if (!_root.hit.hitTest(_root._xmouse+b.xmax-1, _root._ymouse+b.ymax-1, true)) {
_x = _root._xmouse;
_y = _root._ymouse;
}
}
}
}
}
}
onClipEvent (mouseMove) {
move();
}