2 Replies - 5826 Views - Last Post: 26 January 2012 - 09:22 AM

#1 mXX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 14-April 10

Flash AS3: Running over blocks in my game

Posted 13 January 2012 - 01:09 PM

Hi

Im making a game in flash actionscript 3, almost done but I have some problems...
I'll explaing the game first: you are a character(for the moment a black square) and you have to get as high as possible by jumping on blocks that are falling. What I can do at the moment is jump and have a collision with the blocks (left - right) and jump onto the blocks.

The problem I have is: When 2 blocks fall exactly next to each other (Block A falls next to Block B ) Then I jump on Block A and I want to Run to Block B(just run without jumping) I get something very weird, i can't walk on blocks :s
I'm searching for over a week now and still no progress. So im hope I can get any help here. It's very hard to explain the problem without any visualization so I uploaded the files so u can test for yourselves.
download link: http://www.megaupload.com/?d=JT4158KA
also attached the files in the topic, but it doesnt work..

Im still a beginner, so getting a bit help would be great.. Im stuck at this for over a week :s
I even asked my teacher and he couldn't solve it. So I hope you guys can help

But if you understand my explanation and/or are to lazy to download my files (17kb big :P) here is the code where the problem should be
    package Classes
{
	import flash.events.*;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.ui.Keyboard;
	import Classes.KeyObject;


	public class Player extends MovieClip
	{
		var isJumping:Boolean = false;
		var jumpPower:int = 0;
		var ground:int;
		var stageRef:Stage;
		var key:KeyObject;
		var isDead:Boolean = false;
		var allBlocks:Array;
		var blockX:int;
		var blockY:int;
		var blockWidth:int;
		var blockHeight:int;
		var steps:int = 10;
		var upPressed:Boolean = false;
		public function Player(stageReff:Stage,allBlocks:Array)
		{

			stageRef = stageReff;
			key = new KeyObject(stageRef);
			this.allBlocks = allBlocks;
			ground = stageRef.stageHeight;
			addEventListener(Event.ENTER_FRAME,update);
			//addEventListener(KeyboardEvent.KEY_UP, keyReleased);
		}

		function isHit()
		{
			for (var j = 0; j < allBlocks.length; j++)
			{
				if (this.hitTestObject(allBlocks[j]) && allBlocks[j] != isOnTopOf())
				{
					blockX = allBlocks[j].x;
					blockY = allBlocks[j].y;
					blockWidth = allBlocks[j].width;
					blockHeight = allBlocks[j].height;
					return true;
				}
			}
			return false;
		}

		function isHitTop()
		{
			for (var j = 0; j < allBlocks.length; j++)
			{
				if (this.PlayerBottom.hitTestObject(allBlocks[j].hitBoxTop))
				{
					jumpPower = 0;
					isJumping = false;
					this.y = allBlocks[j].y - allBlocks[j].height;
					return true;
				}
			}
			return false;
		}

		function isHitBottom()
		{
			for (var j = 0; j<allBlocks.length; j++)
			{
				if (this.hitTestObject(allBlocks[j].hitBoxBottom))
				{
					if (isJumping)
					{
						this.y = allBlocks[j].y + this.height;
						jumpPower=0;
						isJumping = false;
						isDead = false;
						trace(isDead);
					}
					
					
					else
					{

						isDead = true;
						trace(isDead);
					}
				}
			}
		}

		function isOnTopOf()
		{
			for (var j = 0; j < allBlocks.length; j++)
			{
				if (this.PlayerBottom.hitTestObject(allBlocks[j].hitBoxTop))
				{
					trace("is WEL onTopOf");
					return allBlocks[j];
				}
			}
			trace("is NIET onTopOf");
			return null;
		}


		function update(evt:Event):void
		{
			if (!isDead)
			{
				isHitTop();
				isHitBottom();
				if (key.isDown(Keyboard.UP))
				{
					//Voorkomen dat hij 2x springt
					if (! isJumping)
					{
						jumpPower = 12;
						isJumping = true;
					}
				}

				if (key.isDown(Keyboard.LEFT))
				{
					if (this.x > stageRef.stageWidth - stageRef.stageWidth)
					{
						this.x -=  steps;
						if (isHit() == true)
						{
							this.x = blockX + blockWidth + 1;
							//trace("is gehit maar staat er niet op");
						}

					}
				}
				else if (key.isDown(Keyboard.RIGHT))
				{
					if (this.x < stageRef.stageWidth - this.width)
					{
						this.x +=  steps;
						if (isHit() == true)
						{
							this.x = blockX - this.width - 1;
							this.y = blockY;
						}
					}
				}


				if (isJumping)
				{
					this.y -=  jumpPower;
					jumpPower -=  2;
					isHitTop();
					isHitBottom();
				}


				if (! isJumping && ! isHitTop())
				{
					this.y +=  7;
				}


				if (this.y > ground)
				{
					this.y = ground;
					isJumping = false;
				}
			}
			else
			{
				removeEventListener(Event.ENTER_FRAME,update);
			}
		}
	}
}

This post has been edited by mXX: 13 January 2012 - 01:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Flash AS3: Running over blocks in my game

#2 mXX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 14-April 10

Re: Flash AS3: Running over blocks in my game

Posted 17 January 2012 - 05:38 AM

I've solved the problem. The problem was in the function isHit() I also added some new functionality, like a menu, a score, twitter feed.

I'm almost done with the game. Just one more thing to round up, If I get high enough with my character, at a certain point I can't get any higher because I've reached the top of my scene. I'd like to eg. if my character is in 50% of my sceneheight that the scene will 'go up' like that I can go up to infinite in my game and like that my character will never reach the top of my scene. How can i do that?

Here is the update game with the new functionality :) http://www.megaupload.com/?d=TGGTKF5N

tips/tricks or improvements and feedback are always welcome =)
Was This Post Helpful? 0
  • +
  • -

#3 bfdeitch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-January 12

Re: Flash AS3: Running over blocks in my game

Posted 26 January 2012 - 09:22 AM

Try moving the scene rather than the player to give the illusion of movement. That way the player can just stay in the middle the whole time while the scene moves around.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1