classic platform problem

  • ryrocks
  • Newbie
  • Newbie
  • ryrocks
  • Posts: 6

Post 3+ Months Ago

Hey,
I have a character on the stage (_ball_mc) which you control with the arrow keys. I also have a rectangle movieclip on the stage (this). I want this rectangle movieclip to act as a block platform. ie. When the character jumps ontop of it he can walk across it, if he walks into it from the left he cant move left any further, same with the right side.

These movieclips are not being created at runtime, they are on the stage.

My registration point of the character is bottom right,
my registration point of the block is top left

I've used the following ENTERFRAME with not much luck. Stopping the character from moving any further when he walks into the block seems to be ok. I'm having trouble getting him to land on the block without conflicting with the leftDown and rightDown.

As this is such a common gaming element, I figure there must be some documentation on this ?

Code: [ Select ]
if (_findStageInstances)
            {
                _ball_mc = parent.getChildByName("ball_mc") as MovieClip;
                _findStageInstances = false;
            }
            
            if (_ball_mc.hitTestObject(this) && _ball_mc.x >this.x)
            {
                _ball_mc.rightDown = false;
            }
            if (_ball_mc.hitTestObject(this) && _ball_mc.x > (this.x + this.width))
            {
                _ball_mc.leftDown = false;
                _ball_mc.x +=1; //move ball away from block, otherwise Rdown and Ldown are disabled and it gets stuck
            }
            if (_ball_mc.hitTestObject(this) && _ball_mc.y < this.y)
            {
                _ball_mc.falling = false;
                _ball_mc.jump = false;
                _ball_mc.y = (this.y);
            }
  1. if (_findStageInstances)
  2.             {
  3.                 _ball_mc = parent.getChildByName("ball_mc") as MovieClip;
  4.                 _findStageInstances = false;
  5.             }
  6.             
  7.             if (_ball_mc.hitTestObject(this) && _ball_mc.x >this.x)
  8.             {
  9.                 _ball_mc.rightDown = false;
  10.             }
  11.             if (_ball_mc.hitTestObject(this) && _ball_mc.x > (this.x + this.width))
  12.             {
  13.                 _ball_mc.leftDown = false;
  14.                 _ball_mc.x +=1; //move ball away from block, otherwise Rdown and Ldown are disabled and it gets stuck
  15.             }
  16.             if (_ball_mc.hitTestObject(this) && _ball_mc.y < this.y)
  17.             {
  18.                 _ball_mc.falling = false;
  19.                 _ball_mc.jump = false;
  20.                 _ball_mc.y = (this.y);
  21.             }


Any help would be much appreciated!
  • ibrabdo
  • Novice
  • Novice
  • User avatar
  • Posts: 29

Post 3+ Months Ago

try this code but I wrote it using actionscript2 I don't know if it gonna help u if u translate it to actionscript3 but it's working:
u have 2 objects the ball (ball_mc) and the block (Floor)
This code for the ball_mc:

onClipEvent(load){
VelX = 0 ;
VelY = 0 ;
AccY = 3 ;
PosX = _root.ball_mc._x ;
PosY = _root.ball_mc._y ;
ground = 530 ;
}
onClipEvent(enterFrame){
VelX = VelX * 0.7 ;
VelY += AccY ;
PosX += VelX ;
PosY += VelY ;
if(PosY >= ground ){
VelY = Math.abs(VelY) * 0.4 ;
PosY = ground ;
if(Key.isDown(Key.SPACE)){
VelY = -45;
}
}
if(Key.isDown(Key.LEFT)){
VelX = -15 ;
}
if(Key.isDown(Key.RIGHT)){
VelX = 15 ;
}
_root.ball_mc._x = PosX ;
_root.ball_mc._y = PosY ;
if( _parent.Floor.down = 1 ){
this.ground = 530 ;
}
}

And this code for the Floor:

onClipEvent(load){
down = 1 ;
}
onClipEvent(enterFrame){
if(_parent.ball_mc._y <= this._y &&
_parent.ball_mc._y > (this._y - 50) &&
( _parent.ball_mc._x + 10 ) > this._x &&
_parent.ball_mc._x < ( this._x + this._width) ){
_parent.ball_mc.ground = this._y ;
down = 0 ;
}else{
down = 1;
}
}

Try it on Actionscript 2
  • ibrabdo
  • Novice
  • Novice
  • User avatar
  • Posts: 29

Post 3+ Months Ago

This code for the ball_mc:
Code: [ Select ]
onClipEvent(load){
    VelX = 0 ;
    VelY = 0 ;
    AccY = 3 ;
    PosX = _root.ball_mc._x ;
    PosY = _root.ball_mc._y ;
    ground = 530 ;
}
onClipEvent(enterFrame){
    VelX = VelX * 0.7 ;
    VelY += AccY ;
    PosX += VelX ;
    PosY += VelY ;
    if(PosY >= ground ){
            VelY = Math.abs(VelY) * 0.4 ;
            PosY = ground ;
            if(Key.isDown(Key.SPACE)){
                VelY = -45;
            }
        }
    if(Key.isDown(Key.LEFT)){
        VelX = -15 ;
    }
    if(Key.isDown(Key.RIGHT)){
        VelX = 15 ;
    }
    _root.ball_mc._x = PosX ;
    _root.ball_mc._y = PosY ;
    if( _parent.Floor.down = 1 ){
            this.ground = 530 ;
        }
}
  1. onClipEvent(load){
  2.     VelX = 0 ;
  3.     VelY = 0 ;
  4.     AccY = 3 ;
  5.     PosX = _root.ball_mc._x ;
  6.     PosY = _root.ball_mc._y ;
  7.     ground = 530 ;
  8. }
  9. onClipEvent(enterFrame){
  10.     VelX = VelX * 0.7 ;
  11.     VelY += AccY ;
  12.     PosX += VelX ;
  13.     PosY += VelY ;
  14.     if(PosY >= ground ){
  15.             VelY = Math.abs(VelY) * 0.4 ;
  16.             PosY = ground ;
  17.             if(Key.isDown(Key.SPACE)){
  18.                 VelY = -45;
  19.             }
  20.         }
  21.     if(Key.isDown(Key.LEFT)){
  22.         VelX = -15 ;
  23.     }
  24.     if(Key.isDown(Key.RIGHT)){
  25.         VelX = 15 ;
  26.     }
  27.     _root.ball_mc._x = PosX ;
  28.     _root.ball_mc._y = PosY ;
  29.     if( _parent.Floor.down = 1 ){
  30.             this.ground = 530 ;
  31.         }
  32. }


And this code for the Floor:

Code: [ Select ]
onClipEvent(load){
    down = 1 ;
}
onClipEvent(enterFrame){
    if(_parent.ball_mc._y <= this._y &&
     _parent.ball_mc._y > (this._y - 50) &&
     ( _parent.ball_mc._x + 10 ) > this._x &&
        _parent.ball_mc._x < ( this._x + this._width) ){
        _parent.ball_mc.ground = this._y ;
        down = 0 ;
    }else{
        down = 1;
    }
}
  1. onClipEvent(load){
  2.     down = 1 ;
  3. }
  4. onClipEvent(enterFrame){
  5.     if(_parent.ball_mc._y <= this._y &&
  6.      _parent.ball_mc._y > (this._y - 50) &&
  7.      ( _parent.ball_mc._x + 10 ) > this._x &&
  8.         _parent.ball_mc._x < ( this._x + this._width) ){
  9.         _parent.ball_mc.ground = this._y ;
  10.         down = 0 ;
  11.     }else{
  12.         down = 1;
  13.     }
  14. }

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 8 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.