2d-Sidescroller, help me

2d-Sidescroller help me plz

Page 1 of 1

8 Replies - 3781 Views - Last Post: 14 March 2010 - 04:06 PM

#1 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

2d-Sidescroller, help me

Posted 11 February 2010 - 03:28 PM

Hello all, first off, im working on a small project for a 2d sidescroller, and im running into a lot of problems.

One - i cannot figure out how to get collision detection working.
Two - my character doesnt move as smooth as i want it to.
Three - dont know how to get side scrolling working
Four - not sure how to check for children of my level.

Platformer.as


package {

	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;

	public class Platformer extends MovieClip {

		protected var player:Hero;
		protected var floor:Level;
		protected var Ppoint:Point;
		protected var Gpoint:Point;
		protected var LocalGlobal:Point;

		protected var speed:Number=0;
		protected var xspeed:Number=0;
		protected var yspeed:Number=0;
		protected var friction:Number=0.95;
		protected var gravity:Number=0.1;


		var mainJumping:Boolean=false;
		var jumpSpeedLimit:int=15;
		var jumpSpeed:Number=jumpSpeedLimit;

		public static const UP:int=38;
		public static const LEFT:int=37;
		public static const RIGHT:int=39;
		public var STOP:int;
		public static const W:int=87;
		public static const A:int=65;
		public static const D:int=68;

		public function Platformer() {
			player = new Hero();
			floor = new Level();
			this.stage.addChild(floor);
			floor.addChild(player);
			floor.x=0;
			floor.y=300;
			/*floor.player.x = 0;
			floor.player.y = 300;*/
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
		}

		public function frameHandler(e:Event):void {
			var Lpoint:Point;
			var Rpoint:Point;
			Lpoint=new Point(10,300);
			Rpoint=new Point(300,300);
			Ppoint=new Point(player.x,player.y);
			LocalGlobal=floor.localToGlobal(Lpoint);
			Gpoint=new Point(LocalGlobal.x,LocalGlobal.y);

			if (Gpoint.x<Lpoint.x) {
				floor.move(RIGHT);
			} else if (Gpoint.x > Rpoint.x) {
				floor.move(LEFT);
			} else {
				floor.move(STOP);
			}
		}

		public function keyHandler(e:KeyboardEvent):void {

			switch (e.keyCode) {
				case UP :
				case LEFT :
				case RIGHT :
					player.move(e.keyCode);
					break;
				case W :
					player.move(UP);
					mainJump();
					break;
				case A :
					player.move(LEFT);
					xspeed-=speed;
					break;
				case D :
					player.move(RIGHT);
					xspeed+=speed;
					break;
					xspeed*=friction;
					yspeed+=gravity;
					_y+=yspeed;
					_x+=xspeed;
					break;

					if (_root.coin.hitTest(_x,_y,true)) {
						_root.coin._x=Math.random()*400+50;
					}
					if (player.hitTestObject(hitBlock)) {
						//if main is falling down
						if (jumpSpeed>0) {
							//stop jumping
							mainJumping=false;
							//and set the guys coordinates to be on top of the block
							player.y=hitBlock.y-player.height;
							//he's on solid ground
							mainOnGround=true;
							//break out of the loop
						} else {
							jumpSpeed=Math.abs(jumpSpeed);
							//making the main guy get away from the block
							player.y=hitBlock.y+hitBlock.height+1;
						}

					}

			}
		}

		public function mainJump():void {
			//if main isn't already jumping
			if (! mainJumping) {
				//then start jumping
				mainJumping=true;
				jumpSpeed=jumpSpeedLimit*-1;
				player.y+=jumpSpeed;
			} else {
				//then continue jumping if already in the air
				//crazy math that I won't explain
				if (jumpSpeed<0) {
					jumpSpeed*=1-jumpSpeedLimit/75;
					if (jumpSpeed>- jumpSpeedLimit/5) {
						jumpSpeed*=-1;
					}
				}
				if (jumpSpeed>0&&jumpSpeed<=jumpSpeedLimit) {
					jumpSpeed*=1+jumpSpeedLimit/50;
				}
				player.y+=jumpSpeed;
				//if main hits the floor, then stop jumping
				//of course, we'll change this once we create the level
				/*if(player.hitTestObject(Ground)){
				mainJumping=false;
				}*/

				/*if (player.y>=stage.stageHeight-player.height) {
				mainJumping=false;
				player.y=stage.stageHeight-player.height;
				}*/
			}
		}

		public function getPlayer():Hero {
			return this.player;
		}

	}
}




Hero.as

package {

	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;

	public class Hero extends MovieClip {

		public static const IDLE:int=0;
		public static const BLOCKING:int=1;

		public static const MAX_SPEED:int=10;
		public static const MIN_SPEED:int=1;
		public static const MAX_XSPEED:int=10;
		public static const MIN_XSPEED:int=1;
		public static const MAX_YSPEED:int=10;
		public static const MIN_YSPEED:int=1;
		public static const MAX_ACCELERATION:int=10;
		public static const MIN_ACCELERATION:int=1;
		public static const MAX_FRICTION:int=10;
		public static const MIN_FRICTION:int=1;

		protected var xspeed:Number=0;
		protected var yspeed:Number=0;
		protected var acceleration:Number=0;

		protected var currentState:int;
		protected var nextState:int;

		protected var IsDead:Boolean;

		public function Hero() {
			this.currentState=IDLE;
			this.nextState=IDLE;
			this.addEventListener(Event.ENTER_FRAME, frameHandler);
		}

		public function Death() {
			this.gotoAndPlay("dieStart");
		}

		public function frameHandler(e:Event):void {
			if (this.currentLabel == "idleEnd"
			 || this.currentLabel == "deathEnd") {
				this.changeState();
			}
		}

		public function changeState():void {
			this.currentState=this.nextState;
			switch (this.currentState) {
				case IDLE :
					this.gotoAndPlay("idleStart");
					break;
				case BLOCKING :
					this.gotoAndPlay("blockStart");
					this.nextState=IDLE;
					break;
			}
		}

		public function move(dir:int):void {
			switch (dir) {
				case Platformer.UP :
					this.yspeed+=this.acceleration;
					if (this.yspeed>MAX_YSPEED) {
						this.yspeed=MAX_YSPEED;
					}
					if (this.yspeed<MIN_YSPEED) {
						this.yspeed=MIN_YSPEED;
					}
					this.y-=this.xspeed;
					break;
				case Platformer.LEFT :
					this.xspeed+=this.acceleration;
					if (this.xspeed>MAX_XSPEED) {
						this.xspeed=MAX_XSPEED;
					}
					if (this.xspeed<MIN_XSPEED) {
						this.xspeed=MIN_XSPEED;
					}
					this.x-=this.yspeed;
					break;
				case Platformer.RIGHT :
					this.xspeed+=this.acceleration;
					if (this.xspeed>MAX_XSPEED) {
						this.xspeed=MAX_XSPEED;
					}
					if (this.xspeed<MIN_XSPEED) {
						this.xspeed=MIN_XSPEED;
					}
					this.x+=this.yspeed;
					break;
			}
		}



		//getters

		/*public function getIsDead():int {
		return this.IsDead;
		}*/

	}

}




Level.as

package {

	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;

	public class Level extends MovieClip {

		protected var speed:Number=0;

		public function Level() {


		}

		public function move(dir:int):void {
			switch (dir) {
				case Platformer.UP :
					this.y-=this.speed;
					break;
				case Platformer.LEFT :
					this.x-=this.speed;
					break;
				case Platformer.RIGHT :
					this.x+=this.speed;
					break;
			}
		}



	}

}




Block.as

package {

	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;

	//sprites are just movieclips without any frames in them
	public class Block extends Sprite {
		//_root will signify the root of the document
		private var _root:Object;

		public function Block() {
			//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);
		}

		private function eFrame(event:Event):void {

		}
	}
}




Ground.as

package {

	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;

	public class Ground extends MovieClip {

		public function Ground() {


		}


	}

}


This post has been edited by MrKarazy: 11 February 2010 - 03:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: 2d-Sidescroller, help me

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: 2d-Sidescroller, help me

Posted 14 February 2010 - 05:22 PM

i haven't looked closely enoghe at your hit test but i have other segjestions.

for problem two you might try increasing frame rates
for problem three you should move the world around the player instead of the player himself so instead of player.move(up) you would use Platform.move(down) and all other enmities as well

and im more of an as2 guy i havn't looked too far into as3 though i need to so i cant tell you what to do about the children thing
Was This Post Helpful? 1
  • +
  • -

#3 Superduper  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 17-February 09

Re: 2d-Sidescroller, help me

Posted 16 February 2010 - 06:36 PM

The first too problems sound like a framerate thing, try increase it to 24fs or even 30.

Here's a good tutorial on how to build a 2D sidescroller, it solves problems 3 & 4 for you

How to Create a Platform Game in AS3
Was This Post Helpful? 1
  • +
  • -

#4 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

Re: 2d-Sidescroller, help me

Posted 04 March 2010 - 10:18 PM

ah its all a big cluster eff now. i got collisions working...ish.

Basically the game starts out and its like a big earthquake(WTF?!)
and you can hit jump and he will go up, until he hits something, then he will go down forever.

Eff. :surrender:
Was This Post Helpful? 0
  • +
  • -

#5 Aurel300  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 230
  • Joined: 10-November 07

Re: 2d-Sidescroller, help me

Posted 05 March 2010 - 01:57 AM

Heh, sorry, but I did not look *closely* to that code as well - you cannot except that with a giant chunk of code... I mean, giant for a help post...

Anyways, the only way you should be able to solve this is to step debug the whole thing. For example - you could have something like a "inAir" boolean - which would check for ceiling or something. Did you change it back to false when the hero hits the ground? (again, example)
So, put trace everywhere you find it suitable to check for a value - mainly the part with collisions. When you do that, test movie, and jump - wait a second, then close it and look through the values traced out. I think you will find something which is the way it should not be.
Was This Post Helpful? 1
  • +
  • -

#6 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

Re: 2d-Sidescroller, help me

Posted 11 March 2010 - 02:13 PM

View PostAurel300, on 05 March 2010 - 12:57 AM, said:

Heh, sorry, but I did not look *closely* to that code as well - you cannot except that with a giant chunk of code... I mean, giant for a help post...

Anyways, the only way you should be able to solve this is to step debug the whole thing. For example - you could have something like a "inAir" boolean - which would check for ceiling or something. Did you change it back to false when the hero hits the ground? (again, example)
So, put trace everywhere you find it suitable to check for a value - mainly the part with collisions. When you do that, test movie, and jump - wait a second, then close it and look through the values traced out. I think you will find something which is the way it should not be.



OK, ill try and see if i can fix this. idk if it should be this dam hard to make a jump function... ahhhh

thx for helping.
Was This Post Helpful? 0
  • +
  • -

#7 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

Re: 2d-Sidescroller, help me

Posted 12 March 2010 - 08:01 PM

Ok, so what i have going on now, is that my hero, is dropped on to the level(unintentional) and he hits the floor. Then the level still scrolls down, and up, like an earthquake, i can give you files if u want.

collisions work great now.

So ya, additional help required and appreciated.

This post has been edited by MrKarazy: 12 March 2010 - 08:02 PM

Was This Post Helpful? 0
  • +
  • -

#8 Aurel300  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 230
  • Joined: 10-November 07

Re: 2d-Sidescroller, help me

Posted 14 March 2010 - 05:10 AM

Just a blind hit, but it seems like you have some variables doubled... Like - one "velocityY" in hero and one in camera (level) - they have to be synced, but they probably are not. So, only help is - step debug again :D.
Was This Post Helpful? 1
  • +
  • -

#9 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

Re: 2d-Sidescroller, help me

Posted 14 March 2010 - 04:06 PM

View PostAurel300, on 14 March 2010 - 04:10 AM, said:

Just a blind hit, but it seems like you have some variables doubled... Like - one "velocityY" in hero and one in camera (level) - they have to be synced, but they probably are not. So, only help is - step debug again :D.



ya pretty much i had to set my y and x's different in spots. so i got that fixed no more earth quake.

Now all i need to do is get it so that when i collide with my coins, they get removed fro mthe stage and add to my score, which i'm certain that i dont know how to do yet.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1