View Full Version : [AS3] Trying to convert code
Darksydaz
06-23-2009, 04:41 AM
I'm trying to get walls, platforms and ceilings working for a platformer i'm creating. I saw an old platformer in AS1, and i saw this bright idea. Where a loop would add the script to all of the walls, platforms and ceilings on stage. I was having trouble converting it into AS3 code
This is the AS1 Code
for (a=1;a<=10;a++){
_root["mcWALL"+a]._visible=false
_root["mcCEILING"+a]._visible=false
_root["mcPLATFORM"+a]._visible=false
}
xdeath
06-23-2009, 09:06 AM
lol this is actually one of the easier conversions. most of the time it is a lot harder then this. look below for my solution:
for (a=1;a<=10;a++){
MovieClip(root).this["mcWALL"+a].visible=false;
MovieClip(root).this["mcCEILING"+a].visible=false;
MovieClip(root).this["mcPLATFORM"+a].visible=false;
}
i don't see why they used a for loop though, they could have done the job without the loop. but either way this should work.
Darksydaz
06-23-2009, 06:01 PM
1084: Syntax error: expecting identifier before this.
this is the error i'm recieving now... :(
I dunno why xdeath put a "this" in there. I would do something more like this:
MovieClip(root)["mcWALL"+a].visible=false;
//or maybe:
MovieClip(MovieClip(root)["mcWALL"+a]).visible=false;
And be aware that there are some limitations on where you'll be able to access "root" in AS3.
Darksydaz
06-23-2009, 06:50 PM
Perhaps you two could help me, I'm trying to create a formula where it gives all my platforms the same attributes when I add them to the stage (not with ActionScript I might add). by using a For loop... i first saw this done in the example platformer i saw...
for (i=1; i<=10; i++) {
if (this["ground"+i].hitTestObject(joestand)) {
grounded = true;
joestand.y = this["ground"+i].y;
Ymotion = 0;
} else {
grounded = false;
}
}
Now when i added line 2 ("if (this["ground"... ) i began receiving a type error
Error #1010: A term is undefined and has no properties.
at Untitled_fla::MainTimeline/Engine()
this was the original...
if (_root.mcPLAYER.mcCOLLIDER.hitTest(_root["mcPLATFORM"+a]) == true) {
//if the player is touching a platform
if (_root.mcPLAYER.FALLING == true) {
//if the player is falling
_root.mcPLAYER.YMOVEMENT = 0;
//stop them moving down
_root.mcPLAYER._y = _root["mcPLATFORM"+a]._y;
//put the player at the top of the platform
_root.mcPLAYER.GROUNDED = true;
//tell the player that its grounded
}
}
i've created a AS3 replica using examples around and i'm still getting an error... um... help?
Where is the function called "Engine()"? Is that the function that contains the for loop in question?
Throw in a trace(this["ground"+i]) to see if it is the problem.
Darksydaz
06-23-2009, 07:04 PM
Yes that is the function that contains the loop...
the trace comes back undefined... but the character does land on the platform in the test hub when i test it... but when i added (this["ground"+i]) he still works normally, but when i add a second platform and name it "ground2" he doesn't jump anymore...
here is the Engine function...
function Engine(event:Event) {
joestand.y += Ymotion;
Xmotion *= friction;
joestand.x += Xmotion;
if (grounded == false) {
if (falling == false) {
Ymotion *= inversegrav;
} else {
Ymotion *= gravity;
}
if (Ymotion >-1) {
if (falling == false) {
falling = true;
Ymotion = 1;
}
}
}
for (i=1; i<=10; i++) {
if (this["ground"+i].hitTestObject(joestand)) {
if (falling == true) {
grounded = true;
joestand.y = this["ground"+i].y;
Ymotion = 0;
}
} else {
grounded = false;
}
}
}
Like, if you throw a trace right here:
for (i=1; i<=10; i++) {
trace(this["ground"+i],i);
if (this["ground"+i].hitTestObject(joestand)) {
Darksydaz
06-23-2009, 07:15 PM
TypeError: Error #1010: A term is undefined and has no properties.
at Untitled_fla::MainTimeline/Engine()
[object MovieClip] 1
undefined 2
this is what the output window says... and the engine is a "ENTER_FRAME" event...
Darksydaz
06-23-2009, 08:02 PM
NVM... i think i almost have it... it seems the for loop will only work if the correct amount of objects are on the stage... if there are less than that then it tries to put the code on an object that doesn't exist... thanks you for your help... and exposing that flaw in the code...
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.