View Full Version : Stoping Charicter Hovering!
soapo
01-01-2005, 02:12 PM
How do I make it so that a charicter that is a movieclip wont jest walk over a bilding wich is also a movieclip. I wont it to stop when she hits the building not go over it how do i do that.
AndImNotANoJ
01-01-2005, 02:26 PM
im kindah new to flash too but i wil try and hlp here it is put this in your persons movie clip:
[code]
if (this.hitTest(_root.building)) {
this._x -= 5;
}
Freddy
01-01-2005, 02:29 PM
well That'll work if he's moving to the right.
instead: of _x -= 5;
us should try _x *= -2;
that way the charater will bounce away from the building and it doesnt matter which way hes comming from.
soapo
01-01-2005, 02:53 PM
I tried your idea and it dident work.
soapo
01-01-2005, 03:14 PM
I tried this code but when the charicter hits the building the building disapers.
onClipEvent (enterFrame) {
if (_parent.hero.hitTest(this)) {
this._x *= -2;
}
}
Freddy
01-01-2005, 03:51 PM
well instead of: this._x *= -2;
try: _parent.hero._x *= -2;
soapo
01-01-2005, 04:14 PM
Know when i do that the charticter disapers here is the code for the building
onClipEvent (load) {
movespeed = 3;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
_x-= movespeed;
}
if (Key.isDown(Key.LEFT)) {
_x+= movespeed;
}
if (Key.isDown(Key.UP)) {
_y+= movespeed;
}
if (Key.isDown(Key.DOWN)) {
_y-= movespeed;
}
if (_parent.hero.hitTest(this)) {
_parent.hero._x *= -2;
}
}
Im making the bg move and i am adding movieclips that will be invisable over the buildings and trees and stuff so she wont go over them.
DarkReality
01-01-2005, 04:54 PM
Massive mistake. If you change the _x value of a character to something negative, you literally move him off the screen. Your code should look something like this:
onClipEvent (load)
{
movespeed = 3;
}
onClipEvent (enterFrame)
{
if (Key.isDown(Key.RIGHT)) { vx = -movespeed; }
if (Key.isDown(Key.LEFT)) { vx = movespeed; }
if (Key.isDown(Key.UP)) { vy = movespeed; }
if (Key.isDown(Key.DOWN)) { vy = -movespeed; }
this._x += vx;
this._y += vy;
}
And there's where you're going to meet some difficulties with your hittest. You need to figure out which way the object is coming from when it hits something. If it's approaching from the right or the left, you need to change vx, but when it's approaching from the top or bottom, you need to change vy.
The easiest thing to do is to draw a square around each object and turn the sides into separate objects. That way, you know which direction the character had to have come from when she hits a certain wall. Then you can put:
if (_parent.hero.hitTest(this)) {
this.v? *= -1;
}
where the ? would be x or y, depending on which side the character can hit the wall from. Why this.v? instead of _parent.hero.vx or vy? Well, you always change the background. Your character always stays in one place, meaning you don't want to mess with any of his placement (_x/_y) attributes, and you'll notice that the vx belongs to the building, meaning _parent.hero.vx doesn't even exist.
However, that's an incredibly crude hittest which looks unprofessional and can even lead to problems. A very nice one can be found here:
http://oos.moxiecode.com/#Tutorials
which is an incredibly good tutorial for tilebased games in flash. And if you ask me, tilebased games are the easiest and "best-looking-with-little-effort" game types in flash.
arkhan
01-04-2005, 10:54 PM
meh..make more 4 MCs to make the hittet for a MC..thats not good..
onClipEvent (load) {
movespeed = 3;
function hitTrue(x,y){
_root.hero._x =(x*movespeed)
_root.hero._y =(y*movespeed)
}
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
_x-= movespeed;
if(this.hitTest(thing)){
hitTrue(-1,0)
}
}
else if (Key.isDown(Key.LEFT)) {
_x+= movespeed;
if(this.hitTest(thing)){
hitTrue(1,0)
}
}
else if (Key.isDown(Key.UP)) {
_y+= movespeed;
if(this.hitTest(thing)){
hitTrue(0,1)
}
}
else if (Key.isDown(Key.DOWN)) {
_y-= movespeed;
if(this.hitTest(thing)){
hitTrue(0,-1)
}
}
}
I wrote it down on the forum..not ntirelly sure about it all..but Im pretty sure the sistem works just fine..
it simply checks if the hitTest returns trrue and play the function to move him back to his place..
vBulletin® v3.7.3, Copyright ©2000-2009, Jelsoft Enterprises Ltd.