Hey i'm in the process of making a simple platform game and i have the enemy moving left and right but i can make him move up and down. How do i do that? I'll show you the code i have for the right and left.
package{ import flash.display.Sprite; import flash.display.MovieClip; import flash.display.Shape; import flash.display.DisplayObject; import flash.events.*; //sprites are just movieclips without any frames in them public class Enemy extends Sprite{ //_root will signify the root of the document private var _root:Object; //how quickly the enemy can move private var speed:Number; //-1 is left, 1 is right private var direction:int; public function Enemy(){ //this code will only be run once addEventListener(Event.ADDED, beginClass); //this code will constantly be run addEventListener(Event.ENTER_FRAME, eFrame); } private function beginClass(event:Event):void{ //defining the root of the document _root = MovieClip(root); //defining the movement variables of the enemy speed = 5; direction = 1; //drawing the enemy (it'll be a red circle) this.graphics.beginFill(0xFF0000,1); this.graphics.drawCircle(12.5,12.5,12.5); } private function eFrame(event:Event):void{ //MOVING THE ENEMY this.x += speed * direction; //checking if touching any invisible markers for(var i:int=0;i<_root.invisMarkerHolder.numChildren;i++){ //the process is very similar to the main guy's testing with other elements var targetMarkerisplayObject = _root.invisMarkerHolder.getChildAt(i); if(hitTestObject(targetMarker)){ direction *= -1; this.x += speed * direction; } } //TOUCHING THE MAIN CHARACTER if(this.hitTestObject(_root.mcMain)){ //if it touches the main guy while he's jumping //and the guy is falling down, not jumping up if(_root.mainJumping && _root.jumpSpeed > 0){ //kill this guy this.parent.removeChild(this); //and remove listners this.removeEventListener(Event.ENTER_FRAME, eFrame); //and update score _root.mainScore += 500; } else { //otherwise, kill the main character _root.resetLvl(); } } } } }
If the movement is inversed, or if the object moves up when you expect it to move down, and vice versa, it could just be the coordinate system of the program.
Flash uses a coordinate system (or an x-y system) where increasing y moves an object down, while decreasing y moves an object up. So instead of increasing the y position when you want your object to move up, you decrease the y position to move the object up, and vice versa.
without running your code, it looks to me that in the method eFrame you're adding the speed once for the event and if it hits something it adds a negative distance. The thing is, if you hit more than one thing its going to go in reverse.
simplify and test it without the for loop in there and just do the this.y += speed*direction once. Less to try and keep track of. If it doesn't work then, I'd look elsewhere to see if you're mucking about with the x value in another class or somewhere on the timeline.