Forum Topic: Multi-platform platformer

(197 views • 13 replies)

This topic is 1 page long.

<< < > >>
None

Doneveno

Reply To Post Reply & Quote

Posted at: 10/1/09 02:51 PM

Doneveno LIGHT LEVEL 09

Sign-Up: 01/08/04

Posts: 18

So I'm trying to learn AS3. I've already made what can barely be called a platformer (one-frame sprite moving around, rudimentary jumping engine). My problem is with having more than one platform. Right now I'm testing sprite/platform intersection with hitTestObject. This is all well and good, except for...well, here's an example:

if(!dude.hitTestObject(thing){
thing.x += 8;
}

This code checks to see if you're touching a thing, and if not, moves the thing 8 pixels right. That is to say, it's your "walking to the left" routine.

Experienced AS3-ers will already know my problem. I'd have to make a routine for EVERY SINGLE PLATFORM. No thanks. Naming all the platforms the same thing doesn't cut it, and making them all one movie clip just turns them into one giant, mostly invisible platform.

So what do I do? Is there some way I can incorporate that into a for loop and have like...thing.i.x += 8 instead?


None

Diki

Reply To Post Reply & Quote

Posted at: 10/1/09 02:57 PM

Diki NEUTRAL LEVEL 13

Sign-Up: 01/31/04

Posts: 927

Store all the platforms in an array (or an object if you're using OOP) and then just use a simple for loop:

for(i = 0; i < theArray.length; i++)
{
      if(!dude.hitTestObject(theArray[i])
      {
            thing.x += 8;
      }
}

That will loop once for every index in the array (i.e. once for every platform). The variable "i" will be equal to the current iteration of the loop.
This is very rudimentary and not really going to do what you want on its own, but the theory is there.


None

Doneveno

Reply To Post Reply & Quote

Posted at: 10/1/09 03:00 PM

Doneveno LIGHT LEVEL 09

Sign-Up: 01/08/04

Posts: 18

How would I go about storing them into an array in the first place?


None

Diki

Reply To Post Reply & Quote

Posted at: 10/1/09 03:19 PM

Diki NEUTRAL LEVEL 13

Sign-Up: 01/31/04

Posts: 927

The same way you stored them in their current variable.
Like in this example:

var theArray:Array = new Array();
theArray['guy1'] = new Sprite();
theArray['guy2'] = new Sprite();
theArray['guy3'] = new Sprite();

And so on like that.


None

UnknownFury

Reply To Post Reply & Quote

Posted at: 10/1/09 03:27 PM

UnknownFury EVIL LEVEL 26

Sign-Up: 08/10/05

Posts: 6,027

At 10/1/09 02:57 PM, Diki wrote: for(i = 0; i < theArray.length; i++)
{
if(!dude.hitTestObject(theArray[i])
{
thing.x += 8;
}
}

That's a horrible way to do it. Use hitTestPoint, or other form of more accurate collision detection than hitTestObject, and put all the platforms in one MC.

Portfolio(Under construction): UnknownFury.com |
Msn: giant_ak_47@msn.com | Contact: me@unknownfury.com
Follow me on twitter!


None

hucki

Reply To Post Reply & Quote

Posted at: 10/1/09 03:29 PM

hucki NEUTRAL LEVEL 03

Sign-Up: 07/23/09

Posts: 31

Just thought that I should point out that you did:
if(!dude.hitTestObject(theArray[i])

You missed out a ) at the end, resulting in a bracket left open.
What you need to do is:
if(!dude.hitTestObject(theArray[i]))


None

Doneveno

Reply To Post Reply & Quote

Posted at: 10/1/09 06:23 PM

Doneveno LIGHT LEVEL 09

Sign-Up: 01/08/04

Posts: 18

To test this application out, I made a little thing that creates a square when you push a button, nothing else. Code is as follows:

import flash.display.Sprite;
import flash.events.MouseEvent;
var button:MovieClip = new MovieClip();
var platformcontainer:Sprite = new Sprite();
var a:Number = 0;
var b:Number = 0;
var debugtext:TextField = new TextField();
var debugv:Number = 0;
var arrayfun:Array = new Array();
var i:Number = 0;
arrayfun['square1'] = new Sprite();
arrayfun['square2'] = new Sprite();
arrayfun['square3'] = new Sprite();
arrayfun['square4'] = new Sprite();
arrayfun['square5'] = new Sprite();

this.addChild(platformcontainer);
this.addChild(button);
this.addChild(debugtext);

button.graphics.lineStyle(3, 0x992222);
button.graphics.beginFill(0xAA1111);
button.graphics.drawRect(0, 0, 50, 50);
button.graphics.endFill();
button.x = 0;
button.y = 350;

button.addEventListener(MouseEvent.CLICK, makesquare);

function makesquare(e:Event):void{
	arrayfun[i].graphics.lineStyle(3,0x1111AA);
	arrayfun[i].graphics.beginFill(0x222299);
	arrayfun[i].graphics.drawRect(0,0,100,100);
	arrayfun[i].graphics.endFill();
	arrayfun[i].x = a;
	arrayfun[i].y = b;
	platformcontainer.addChild(arrayfun[i]);
	a += 5;
	b += 5;
	debugv++;
	i++;
}

debugtext.x = 100;
debugtext.y = 100;
addEventListener(Event.ENTER_FRAME, display_variable);
function display_variable(event:Event){
	debugtext.text = String (debugv + " " + a + " " + b + " " + platformcontainer.numChildren);
}

But it doesn't work. What did I do wrong?


None

Doneveno

Reply To Post Reply & Quote

Posted at: 10/1/09 07:55 PM

Doneveno LIGHT LEVEL 09

Sign-Up: 01/08/04

Posts: 18

Nobody?


None

Diki

Reply To Post Reply & Quote

Posted at: 10/1/09 08:02 PM

Diki NEUTRAL LEVEL 13

Sign-Up: 01/31/04

Posts: 927

You're not referencing the array properly.
You have:

arrayfun[i].graphics.lineStyle(3,0x1111AA);
	arrayfun[i].graphics.beginFill(0x222299);
	arrayfun[i].graphics.drawRect(0,0,100,100);
	arrayfun[i].graphics.endFill();
	arrayfun[i].x = a;
	arrayfun[i].y = b;
	platformcontainer.addChild(arrayfun[i]);

That is looking for arrayfun[0], arrayfun[1], arrayfun[2], et al, which are not initialized.
You want this:

arrayfun[i].graphics.lineStyle(3,0x1111AA);
	arrayfun['square'+i].graphics.beginFill(0x222299);
	arrayfun['square'+i].graphics.drawRect(0,0,100,100);
	arrayfun['square'+i].graphics.endFill();
	arrayfun['square'+i].x = a;
	arrayfun['square'+i].y = b;
	platformcontainer.addChild(arrayfun['square'+i]);

arrayfun['square'+i] will end up being arrayfun['square1'], arrayfun['square2'], et cetera.
However I didn't actually run the code so there in theory could be something else wrong with it.


None

Diki

Reply To Post Reply & Quote

Posted at: 10/1/09 08:03 PM

Diki NEUTRAL LEVEL 13

Sign-Up: 01/31/04

Posts: 927

Whoops, missed a line.
Proper code:

arrayfun['square'+i].graphics.lineStyle(3,0x1111AA);
	arrayfun['square'+i].graphics.beginFill(0x222299);
	arrayfun['square'+i].graphics.drawRect(0,0,100,100);
	arrayfun['square'+i].graphics.endFill();
	arrayfun['square'+i].x = a;
	arrayfun['square'+i].y = b;
	platformcontainer.addChild(arrayfun['square'+i]);

None

Doneveno

Reply To Post Reply & Quote

Posted at: 10/1/09 09:51 PM

Doneveno LIGHT LEVEL 09

Sign-Up: 01/08/04

Posts: 18

Ah, see, I'm used to C++ where array[0] means the first thing in an array. AS3 is the first language I've seen where you refer to things in an array by name. Thanks.


None

kizza0

Reply To Post Reply & Quote

Posted at: 10/1/09 10:01 PM

kizza0 NEUTRAL LEVEL 08

Sign-Up: 10/13/07

Posts: 276

you can use numbers aswell.

BOOM HEADSHOT

BBS Signature

None

Diki

Reply To Post Reply & Quote

Posted at: 10/1/09 10:16 PM

Diki NEUTRAL LEVEL 13

Sign-Up: 01/31/04

Posts: 927

At 10/1/09 09:51 PM, Doneveno wrote: Ah, see, I'm used to C++ where array[0] means the first thing in an array. AS3 is the first language I've seen where you refer to things in an array by name. Thanks.

It's the same in AS3. However you can also refer to the arrays using keys that are strings, such as in the example I have you. However if you use the string keys to define the array like you did you must reference them the say way.

If you wanted to do it the same as C++ you can either just use numbers instead of "square#" or use arrayfun.push(value) which will add on an index to the end of the array, and can be referenced using the appropriate index number.


None

blank0000

Reply To Post Reply & Quote

Posted at: 10/2/09 01:35 AM

blank0000 LIGHT LEVEL 07

Sign-Up: 01/29/06

Posts: 240

Well I just go into whatever I have as a ground and manipulate the symbol to be a bunch of platforms.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 02:18 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!