applying global function to movieclip

  • arliita
  • Born
  • Born
  • arliita
  • Posts: 1

Post 3+ Months Ago

I'm creating a game where the character stays in the middle of the screen and the background scrolls past as buttons are pressed. I have several enemies and such that I want to scroll into view as well.
I created a global function:

Code: [ Select ]
_global.objects_move = function()
{
if (Key.isDown(Key.UP)) {
this._y -= 10;
}
if (Key.isDown(Key.DOWN)) {
this._y += 10;
}
if (Key.isDown(Key.LEFT)) {
this._x += 10;
}
if (Key.isDown(Key.RIGHT)) {
this._x -= 10;
}
};
  1. _global.objects_move = function()
  2. {
  3. if (Key.isDown(Key.UP)) {
  4. this._y -= 10;
  5. }
  6. if (Key.isDown(Key.DOWN)) {
  7. this._y += 10;
  8. }
  9. if (Key.isDown(Key.LEFT)) {
  10. this._x += 10;
  11. }
  12. if (Key.isDown(Key.RIGHT)) {
  13. this._x -= 10;
  14. }
  15. };


and I want to apply to all the different enemy movieclips so they scroll into view.
I've tried adding this code onto the movieclips

Code: [ Select ]
onClipEvent (enterFrame)
{
this.objects_move();
}
  1. onClipEvent (enterFrame)
  2. {
  3. this.objects_move();
  4. }


and similar things but I just can't get it to work...
Moderator Remark: Added code tags
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ibrabdo
  • Novice
  • Novice
  • User avatar
  • Posts: 29

Post 3+ Months Ago

try this :
Code: [ Select ]
onClipEvent(load){
    VelX = 0 ; // world velocity on x axis
    VelY = 0 ; // world velocity on y axis
    PosX = _root.BackGround._x ; // the background position on x axis
    PosY = _root.BackGround._y ; // the background position on y axis
    PosX2 = _root.enemie._x; // the enimie position on x axis
    PosY2 = _root.enemie._y; // the enimie position on y axis
}
onClipEvent(enterFrame){
    VelX = VelX * 0.5;
    VelY = VelY * 0.5;
    PosX += VelX ;
    PosY += VelY ;
    PosX2 += VelX ;
    PosY2 += VelY ;
    if(Key.isDown(Key.LEFT)){
        VelX += 5;
    }
    if(Key.isDown(Key.RIGHT)){
        VelX -= 5;
    }
    if(Key.isDown(Key.UP)){
        VelY += 10;
    }
    if(Key.isDown(Key.DOWN)){
        VelY -= 10;
    }
    _root.BackGround._x = PosX;
    _root.BackGround._y = PosY;
    _root.enemie._x = PosX2;
    _root.enemie._y = PosY2;
}
  1. onClipEvent(load){
  2.     VelX = 0 ; // world velocity on x axis
  3.     VelY = 0 ; // world velocity on y axis
  4.     PosX = _root.BackGround._x ; // the background position on x axis
  5.     PosY = _root.BackGround._y ; // the background position on y axis
  6.     PosX2 = _root.enemie._x; // the enimie position on x axis
  7.     PosY2 = _root.enemie._y; // the enimie position on y axis
  8. }
  9. onClipEvent(enterFrame){
  10.     VelX = VelX * 0.5;
  11.     VelY = VelY * 0.5;
  12.     PosX += VelX ;
  13.     PosY += VelY ;
  14.     PosX2 += VelX ;
  15.     PosY2 += VelY ;
  16.     if(Key.isDown(Key.LEFT)){
  17.         VelX += 5;
  18.     }
  19.     if(Key.isDown(Key.RIGHT)){
  20.         VelX -= 5;
  21.     }
  22.     if(Key.isDown(Key.UP)){
  23.         VelY += 10;
  24.     }
  25.     if(Key.isDown(Key.DOWN)){
  26.         VelY -= 10;
  27.     }
  28.     _root.BackGround._x = PosX;
  29.     _root.BackGround._y = PosY;
  30.     _root.enemie._x = PosX2;
  31.     _root.enemie._y = PosY2;
  32. }


if u want the enemie the move while he's scrolling u have to give him a difrent VelX (VelXE)VelXE = 5 * dir (dir is the direction of the enemie 1 or -1 ) then the total move ( VelXT )gonna be
VelXT = VelXE + VelX
and the same thing on the _y axis

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 25 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
 
cron
 

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