Be a Supporter!

addChild Position Player exactly??

  • 148 Views
  • 8 Replies
New Topic Respond to this Topic
Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 22
Game Developer
addChild Position Player exactly?? 2013-12-21 04:01:32 Reply

So I'm doing a little demo game to test some things out (still pretty new at this). For this test I'm not using multiple classes like I've done before - not even a player class. I am just putting everything into the Main class (doc class). I know it's best to not do it this way but for the sake of my test I went with it this time around...

I have one MC (LevelOne) and addChild that to the stage. Inside LevelOne is a background MC which contains all the other elements of the game. Then I addChild the Player to LevelOne.

I was just trying to setup movement with parallax scrolling which I got to work and am learning to understand it. However, the problem is that I can't addChild the Player to a specific location inside the level.

I've tried everything I could think of for the past 3 hours. Player.x = (whatever value) or many other variations of this. I know I don't fully understand parallax scrolling so maybe it is something I am missing in that area of coding. I made the mistake of taking 3 months off of learning coding to focus on filmmaking (it's what I do) and it kinda screwed what progress I was making.

Here's the code:

package
{
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.events.Event;
	import flash.events.MouseEvent;

	
	public class Main extends MovieClip
	{
		//Add var to stage
		var levelOne:LevelOne;
		var player:Player;
		
		var vx:int;
		var vy:int;
		var rightInnerBoundary:uint;
		var leftInnerBoundary:uint;
		var topInnerBoundary:uint;
		var bottomInnerBoundary:uint;
	
		public function Main()
		{
			init();
		}
		function init():void
		{
			//Initialize variables
			levelOne = new LevelOne();
			addChild(levelOne);
			
			
			player = new Player();
			levelOne.addChild(player);
			//THIS IS WHERE I TRIED POSITIONING PLAYER BUT NO LUCK WITH WHATEVER I TRIED
			
			rightInnerBoundary = (stage.stageWidth / 2) + (stage.stageWidth / 4);
			leftInnerBoundary = (stage.stageWidth / 2) - (stage.stageWidth / 4);
			topInnerBoundary = (stage.stageHeight / 2) - (stage.stageHeight / 4);
			bottomInnerBoundary = (stage.stageHeight / 2) + (stage.stageHeight / 4);
			
			//Players velocity
			vx = 0;
			vy = 0;
			
			
			//Add event Listeners
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);

		}
		function onKeyDown(event:KeyboardEvent):void
		{
			if (event.keyCode == Keyboard.A)
			{
				vx = -6;
			}
			else if (event.keyCode == Keyboard.D)
			{
				vx = +6;
			}
			else if (event.keyCode == Keyboard.W)
			{
				vy = -6;
			}
			else if (event.keyCode == Keyboard.S)
			{
				vy = +6;
			}
		}
		function onKeyUp(event:KeyboardEvent):void
		{
			if (event.keyCode == Keyboard.A || event.keyCode == Keyboard.D)
			{
				vx = 0;
			}
			else if (event.keyCode == Keyboard.S || event.keyCode == Keyboard.W)
			{
				vy = 0;
			}
		}
		function onEnterFrame(event:Event):void
		{
			//Initialize local variables
			var playerHalfWidth:uint = player.width / 2;
			var playerHalfHeight:uint = player.height / 2;
			var backgroundHalfWidth:uint = levelOne.background.width / 2;
			var backgroundHalfHeight:uint = levelOne.background.height / 2;
			
			//Move the player
			player.x += vx;
			player.y += vy;
			
			//Stop player at inner boundary edges
			if (player.x - playerHalfWidth < leftInnerBoundary)
			{
				player.x = leftInnerBoundary + playerHalfWidth;
				rightInnerBoundary = (stage.stageWidth / 2) + (stage.stageWidth / 4);
				levelOne.background.x -= vx;
			}
			else if (player.x + playerHalfWidth > rightInnerBoundary)
			{
				player.x = rightInnerBoundary - playerHalfWidth;
				leftInnerBoundary = (stage.stageWidth / 2) - (stage.stageWidth / 4);
				levelOne.background.x -= vx;
			}
			if (player.y - playerHalfHeight < topInnerBoundary)
			{
				player.y = topInnerBoundary + playerHalfHeight;
				bottomInnerBoundary = ( stage.stageHeight / 2) + (stage.stageHeight / 4);
				levelOne.background.y -= vy;
			}
			else if (player.y + playerHalfHeight > bottomInnerBoundary)
			{
				player.y = bottomInnerBoundary - playerHalfHeight;
				topInnerBoundary = (stage.stageHeight / 2) - (stage.stageHeight / 4);
				levelOne.background.y -= vy;
			}
			//Stop background at stage edges
			if (levelOne.background.x + backgroundHalfWidth < stage.stageWidth)
			{
				levelOne.background.x = stage.stageWidth - backgroundHalfWidth;
				rightInnerBoundary = stage.stageWidth;
			}
			else if (levelOne.background.x - backgroundHalfWidth > 0)
			{
				levelOne.background.x = 0 + backgroundHalfWidth;
				leftInnerBoundary = 0;
			}
			if (levelOne.background.y - backgroundHalfHeight > 0)
			{
				levelOne.background.y = 0 + backgroundHalfHeight;
				topInnerBoundary = 0;
			}
			else if (levelOne.background.y + backgroundHalfHeight < stage.stageHeight)
			{
				levelOne.background.y = stage.stageHeight - backgroundHalfHeight;
				bottomInnerBoundary = stage.stageHeight;
			}
			
		}
	}
}

BBS Signature
Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 22
Game Developer
Response to addChild Position Player exactly?? 2013-12-21 04:27:32 Reply

The stage is 600x500. LevelOne is 1200x1000. No matter what value I try when I position the Player, for example
player.x =800 player.y ==800, the Player just ends up close to the top left of LevelOne.


BBS Signature
Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 22
Game Developer
Response to addChild Position Player exactly?? 2013-12-21 04:52:09 Reply

I really should just let it rest till someone gives some input but I've been at this for hours and it's driving me mad...

I guess I don't know shit about parallax scrolling.

I just made a different test. Just used to layers on main timeline. One was a background MC and other was Player. Didn't even use addChild I just physically but them there.

No matter how I tried I couldn't position the player anywhere that I wanted....


BBS Signature
Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to addChild Position Player exactly?? 2013-12-21 05:38:29 Reply

I haven't got time to go through and debug your code and stuff. But on a quick glance, and based on what your problem is, one of these conditions is being met:

if (player.x - playerHalfWidth < leftInnerBoundary)
			{
				player.x = leftInnerBoundary + playerHalfWidth;
				rightInnerBoundary = (stage.stageWidth / 2) + (stage.stageWidth / 4);
				levelOne.background.x -= vx;
			}
			else if (player.x + playerHalfWidth > rightInnerBoundary)
			{
				player.x = rightInnerBoundary - playerHalfWidth;
				leftInnerBoundary = (stage.stageWidth / 2) - (stage.stageWidth / 4);
				levelOne.background.x -= vx;
			}
			if (player.y - playerHalfHeight < topInnerBoundary)
			{
				player.y = topInnerBoundary + playerHalfHeight;
				bottomInnerBoundary = ( stage.stageHeight / 2) + (stage.stageHeight / 4);
				levelOne.background.y -= vy;
			}
			else if (player.y + playerHalfHeight > bottomInnerBoundary)
			{
				player.y = bottomInnerBoundary - playerHalfHeight;
				topInnerBoundary = (stage.stageHeight / 2) - (stage.stageHeight / 4);
				levelOne.background.y -= vy;
			}

As this is the only place you ever set the players x and y properties.

Parallax scrolling or just level scrolling in general, you should have a main container that holds everything (sorry if this is not much help and you already know most of it anyway). The player never leaves the stage, it's an illusion that it's moving around the level. Set the bounds for the player, and allow the player to move within the bounds. If the player leaves the bounds, the level should move instead. Because of this, the parent of the player should always be at 0,0 (for simplicity's sake) and never move. In your example you add the player to the level and also move the level. The player should move independently of the level. The level assets should then be added to the main container (I usually also have another container to hold all the level assets).

In the end it's something like this:

for each(asset in levelAssets)
{
    levelContainer.addChild(asset); //You might not do it this way, just demonstrating the levelContainer has all the assets in
}

mainContainer.addChild(levelContainer);
mainContainer.addChild(player);

// Your loop or whatever
if(player is within its bounds)
{
    // player reacts to input
}
else
{
   // levelContainer reacts to input instead
}

Sorry for being vague. I'll check back in on this thread later when I've got a bit more time. Good luck!

kkots
kkots
  • Member since: Apr. 16, 2013
  • Offline.
Forum Stats
Supporter
Level 10
Blank Slate
Response to addChild Position Player exactly?? 2013-12-21 11:40:28 Reply

At 12/21/13 04:52 AM, Hero101 wrote: I really should just let it rest till someone gives some input but I've been at this for hours and it's driving me mad...

What are you talking about?

I guess I don't know shit about parallax scrolling.

If you don't know anything about parallax scrolling, then make a test without it.
If it works, then add parallax scrolling into it.
I have participated in a thread oriented on parallax scrolling, by the way:
Hello all, quick question (yeaaaah, reeeaaally quick...)

I just made a different test. Just used to layers on main timeline. One was a background MC and other was Player. Didn't even use addChild I just physically but them there.

That'd bad. Export all the things to classes, remove them from the stage in Flash IDE, instantiate them in code, and addChild them. And make sure every clip has origin point in top left corner. The player must have origin point between his feet.

No matter how I tried I couldn't position the player anywhere that I wanted....

Try to position it in center.
Or write a camera class, and lock the camera position to the player.
Here I participated in a thread dealing with camera programming
http://www.newgrounds.com/bbs/topic/1349935
So you could put your parallax code into the camera class.

Also, how exactly do you want your parallax to work? Do you want the parallax to activate only when the player leaves a certain border?
Anyway, I think the player must be able to freely move within the game's space, but it must reside inside a container, position of which is controlled by the camera, and the camera's target is locked to player's position.
How do you like that?


BBS Signature
Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 22
Game Developer
Response to addChild Position Player exactly?? 2013-12-21 22:30:21 Reply

:In your example you add the player to the level and also move the level. The player should move independently of the level. The level assets should then be added to the main container (I usually also have another container to hold all the level assets).

I didn't now that the player moved independently of the level. I thought that parallax scrolling gave the illusion of the player moving but it was actually the level moving (at least with what I'm doing which is that top down view kinda game).

What I'm trying to do is have the player be able to roam freely within the limits of a "boundary box" and once the player hits the edge of this boundary box and keeps walking then the level moves instead of the player. Only once the player reaches the end of the stage can he walk outside his boundary box and touch the edge of the level. Don't know if I explained that well enough...

I really appreciate all of your feedback. It's very comprehensive and I will definitely be referring back to it. I'm still have a long way to understanding code. You see I have yet to learn about Arrays (know what they are and understand why you need them I just haven't learned how to code them) or even physics (i.e. making your character jump).

I do have a amateur question for you... In your examples you mentioned containers. Now I do understand what containers are for and why we need them, yet when ever someone suggests in these forums (to my post or others that I read) I am at a loss.

Is there any way you could explain containers simply to me? I've seen levelContainer, gameContainer, mainContainer and many more. What do they all mean or consist of? How exactly?

Thanks in advance.


BBS Signature
MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Online!
Forum Stats
Supporter
Level 16
Game Developer
Response to addChild Position Player exactly?? 2013-12-21 22:44:13 Reply

At 12/21/13 10:30 PM, Hero101 wrote: Is there any way you could explain containers simply to me? I've seen levelContainer, gameContainer, mainContainer and many more. What do they all mean or consist of? How exactly?

Container = something that extends DisplayObjectContainer, usually just a Sprite. level-, game-, main- are just the names we use for that sprite to suggest what its purpose is.

Hero101
Hero101
  • Member since: Dec. 13, 2005
  • Offline.
Forum Stats
Supporter
Level 22
Game Developer
Response to addChild Position Player exactly?? 2013-12-21 23:06:09 Reply

Container = something that extends DisplayObjectContainer, usually just a Sprite. level-, game-, main- are just the names we use for that sprite to suggest what its purpose is.

Oh ok gotcha. You know you, MSGhero, and kkots are always helping me out on the forums and the knowledge that you guys possess with AS3, and coding in general, is incredible. I've only been at this on and off for the past year. This next year I plan to work on it consistently. Everything I've learned so far is through a book I bought. It only has 3 more chapters left that have to do with physics, arrays (looking forward to learning about those) and AI.

So I'm really curious where I should look to continue my learning? Where did you guys learn this craft? I don't know if I should find another book or just do endless online tutorials... I don't want to just learn the code but also to fully understand it.


BBS Signature
kkots
kkots
  • Member since: Apr. 16, 2013
  • Offline.
Forum Stats
Supporter
Level 10
Blank Slate
Response to addChild Position Player exactly?? 2013-12-22 07:25:41 Reply

At 12/21/13 11:06 PM, Hero101 wrote: So I'm really curious where I should look to continue my learning? Where did you guys learn this craft? I don't know if I should find another book or just do endless online tutorials... I don't want to just learn the code but also to fully understand it.

Is your problem solved?

I used Youtube (5%), Kirupa (10%), Google (80%), NG Flash Forum (5%). No books. No teachers. Only me, Internet, and Flash 8.


BBS Signature