ForumsProgramming ForumAS2 Moving Platforms

15 7434
DapwnageG
offline
DapwnageG
205 posts
Nomad

I am working on a platform game, and I can't seem to make the player stick onto moving platforms.

All of the platforms are in one movie clip called "level" and I added an animation in a movie clip for moving platforms inside the level. The player needs to stick onto the moving platforms, rather than remaining in the same position until the platform moves from under him.

Also, the platforms just won't be moving side to side or up and down, they will be moving all over the place in a random direction.

Thanks in advance.

  • 15 Replies
markzi
offline
markzi
103 posts
Nomad

Easy Fix: Make the platform a half rectangle so the platform will have sides. That way when the platforms move there will be a wall blocking the character and preventing it from falling. Hope I helped,I can make it clearer for you if did not understand what I meant. Good luck.

DapwnageG
offline
DapwnageG
205 posts
Nomad

It would work, But I also want the player to be able to move on the platform and jump as it is moving.

markzi
offline
markzi
103 posts
Nomad

Oh, I would love to have that in one of My games. But I am not skilled enough. You will have to ask a more experienced developer like John.

abarrow
offline
abarrow
22 posts
Nomad

The way I have coded moving platforms is to have them move your character, by calling code onEnterFrame.

as 2:

if(this.hitTest(person._x, person._y+10, true)){
person._x+=this.vx;
person._y+=this.vy;
}

as 3:

if(event.target.hitTestPoint(person.x, person.y+10, true)){
person.x+=event.target.vx;
person.y+=event.target.vy;
}

DapwnageG
offline
DapwnageG
205 posts
Nomad

Sorry doesn't work. I understand almost all of the code, but what do the vx and vy mean?

abarrow
offline
abarrow
22 posts
Nomad

vx and vy are example variables that represent the platform's x and y velocities.

as 3: horizontal moving platform

platform.vx=5;

platform.maxX=200;
platform.minX=50;

platform.addEventListener(Event.ENTER_FRAME, enter);

function enter(event:Event):void{
//move person
if(event.target.hitTestPoint(person.x, person.y+10, true)){
person.x+=event.target.vx;
}
//move platform
event.target.x+=event.target.vx;
//switch direction
if(event.target.x>event.target.maxX){
event.target.vx*=-1;
}else if(event.target.x<event.target.minX){
event.target.vx*=-1;
}
}

See http://abarrow.ca/hosting/horizontalplatform.swf
and http://abarrow.ca/hosting/horizontalplatform.fla

DapwnageG
offline
DapwnageG
205 posts
Nomad

Actually I found it out on my own after experimenting.
onClipEvent (load) {
distx = 0;
disty = 0;
}
onClipEvent (enterFrame) {
if (_root.Player.hitTest(this)) {
_root.Player._x = this._x+distx;
_root.Player._y = this._y-disty;
if (Key.isDown(65)) {
distx -= _root.Player.speed;
}
if (Key.isDown(68)) {
distx += _root.Player.speed;
}
} else {
distx = _root.Player._x-this._x;
disty = _root.Player._y-this._y;
}
}
I was so relieved when I got this down. All on my own too, so it is even more satisfying. I hope other people could use this script for help to.

Ongokiller50
offline
Ongokiller50
12 posts
Nomad

You Need to set the players Speed With the platform speed.

if the platform is A movieclip animation. You need to make it move with a var.
Like on the platform


this._x = this._x+xspeed
xspeed = xspeed

xspeed = 10

thats moving the platfrome using a var

THEN when the player hits the platfrome U put on the platfrom


if (_root.player.hitTest(this)){
_root.player.xspeed = this.xspeed
}


what it does is when The player hits the platfrom the player var xspeed(witch is his X "LEFT","RIGHT&quot Moves the same speed as the platfrom.

DapwnageG
offline
DapwnageG
205 posts
Nomad

Bump.

Basically, I made a movie clip of the platform, and put that platform movie clip in another movie clip where it is being moved around (The player is not in the movie clip). My script only works when the platform isn't in a movie clip. While experimenting, I found a command called _root.localToGlobal and _root.globalToLocal. On the flash help window, it said that it can be used to translate x coordinates from in a movie clip to outside a movie clip, and vise versa. Is there anyway I could fit that code into the script to make it work?

Keep in mind before posting, IT DOES NOT HAVE A CERTAIN X OR Y SPEED. It is just an animation and I am trying to get the player static to the platform while on top of it.

abarrow
offline
abarrow
22 posts
Nomad

On the subject of hitTest it must take in global coordinates, the easiest way to find these is the following.

AS 2:

var po={x:0, y:0};
this.localToGlobal(po);
if(blah.hitTest(po.x, po.y, true)){
}

DapwnageG
offline
DapwnageG
205 posts
Nomad

Does the localToGlobal convert the variable itself, or what?

I don't need the hitTest to work. I need the x and y coordinates of the moving platform to be in global coordinates, so the distx and disty variable will be correct. The formula goes like this:

distx = _root.Player._x-xpos;
disty = _root.Player._y-ypos;

How could I translate the platforms x and y coord to be in global coord, rather than the movie clips coord?

abarrow
offline
abarrow
22 posts
Nomad

Yes, localToGlobal rather than returning a value, changes the value of the point.

localToGlobal changes a point form the local coordinate scheme (Where 0,0 is the center of the MovieClip), into the _root coordinate scheme where 0,0 is the top left corner. This function is mainly used when one or more parent MovieClips is being rotated. If you know all the parents of a MovieClip and they are not being scaled or rotated, then summing there coordinates will get you the global coordinates.

var globalX:Number=granddad.x+dad.x+son.x;

or

var po={0,0};
son.localToGlobal(po);
var globalX:Number=po.x;

DapwnageG
offline
DapwnageG
205 posts
Nomad

The script is on a platform, which is in a movieclip.

onClipEvent(enterFrame){
var pos={x: _x, y: _y}
this.localToGlobal(pos);
distx=_root.Player._x-pos.x;
disty=_root.Player._y-pos.y;
}


Would pos.x and pos.y be in global coordinates or the movie clip coordinates? When I trace distx, it is way off. Maybe the equation is wrong?

abarrow
offline
abarrow
22 posts
Nomad

Complete Example:

this._x=5;
this._parent._x=50;
this.child.x=0.5;

var po={x:0, y:0};
this.child.localToGlobal(po);
trace(po.x);//55.5

po={x:0, y:0};
this.parent.localToGlobal(po);
trace(po.x);//50

po={x:0, y:0);
this.localToGlobal(po);
trace(po.x)//55

The code you should be using is:

onClipEvent(enterFrame){
var pos={x:0, y:0}
this.localToGlobal(pos);
var distx:Number=_root.Player._x-pos.x;
var disty:Number=_root.Player._y-pos.y;
}

DapwnageG
offline
DapwnageG
205 posts
Nomad

Oh thanks so much, I have been trying to solve this problem forever.

Showing 1-15 of 15