View Full Version : [AS3] - LocalToGlobal Help
aaron_da_killa
02-26-2009, 09:23 AM
Hey
For my platform game, I need to perform collision detection (including realigning the player along the x/y axis) between my platform and the player (both movieclips). However, the platform is located inside a movieclip named level2. This isn't a problem with collision detection however when I want to re-align the player to the top of the platform, it realigns the player to the top of the platform's local co-ordinates (in regards to the top left of the movieclip "level2") however I need to realign the player ontop of the platform's global co-ordinates (in regards to the top left of the stage).
I've been told I need to use localtoglobal but after an hour or so researching and trying to make sense of the code I'm left absouloutley clueless. I was wondering if somebody could explain it to me a bit in simple terms and perhaps show me what I have to do. Here is my code for when the player hits the top of the platform:
//TOP SIDE
if (player.y + player.height <= level2.platform.y + level2.platform.height/2 && player.x + player.width > level2.platform.x + 8 && player.x < level2.platform.x + level2.platform.width - 8){
playerJumping = false;
player.y = level2.platform.y - player.height;
}
The second last line is what is important here as this is where I adjust the player's position so he is sitting nicely on the platform. Although this code works, it realigns the player ontop of the platform's local coordinates which is no use to me. So how do I get the player to sit nicely ontop of the platform's global coordinates????
bluemagica
02-26-2009, 09:51 AM
player.y=localToGlobal(new Point(level2.platform.x,level2.platform.y)).y-player.height;
I am not so good at this, but your code should work something like that!
aaron_da_killa
02-27-2009, 03:04 AM
player.y=localToGlobal(new Point(level2.platform.x,level2.platform.y)).y-player.height;
I am not so good at this, but your code should work something like that!
Thanks bluemagica ;)
I tried the code you said but it seems to have no effect at all. In otherwords:
player.y = level2.platform.y - player.height;
and
player.y=localToGlobal(new Point(level2.platform.x,level2.platform.y)).y-player.height;
seem to position the player in the exact same spot. The localToGlobal in your code you posted dosen't work I think. I've been manipulating that line of code but have had no success. I'm going to keep trying when I get back home.
If anybody has any ideas please let me know.
bluemagica
02-27-2009, 03:09 AM
hmm can you upload a test fla(remove unneccesary stuff as much as possible)
aaron_da_killa
02-27-2009, 08:42 AM
Thanks bluemagica (and all the other folks), I appreciate all the help you have given me in this and other threads. :)
I've had to upload the file to my FileFront account as it seems there is a 500kB limit for attachments here.
Here is the download link (http://files.filefront.com/Multimedia+Major+Projectrar/;13375883;/fileinfo.html)
(the game is only a prototype so all the art assets are really rough)
aaron_da_killa
02-27-2009, 11:50 AM
Double Post:
Holy shit, after a few hours or internet searching and hopelessly trying I have found out the answer! Brace yourself:
player.y = (level2.platform.parent.localToGlobal(new Point(level2.platform.x, level2.platform.y)).y) - player.height;
So why did it work when I added in "level2.platform.parent."? What does parent mean?
Also another question, how efficient is this code? Are these so called new points temporary as such, will they consume alot of performance if this code is applied to several other platforms?
EightySeven
02-27-2009, 03:07 PM
var point:Point = new Point(a,b);
where as a and b are the local points of the object;
...new Point(enemy.x, enemy.y);
point.localToGlobal(point);
This converts your point to its global cordinates
Once this is done you refer to this new point instead of the original object(enemy)
so you would now use
point.x and point.y to refer to your enemy.x and enemy.y on a global value
IMO your code is slightly confusing...converting the player.y to global like that But i have slight OCD and I like to be able to clearly see where my values are at..i find it easier to debug as well. in my code if i wasn't sure or want to make sure it was funcitoning correctly I would just
trace(point); where as you will have alot of code to feed to trace it correctly
think of the hire achy of flash as a family.
Think of the stage(root) as a god the source of all.
Anything you put on the stage is now a child of god
So if you created a file and saved it as EightySeven and put a a MovieClip on the stage and called it Aaron. if you traced Aaron.parent it would most likely output "EightySeven"
Now if you open up the Aaron MovieClip and droped a graphic on there and called it bluemagica and traced bluemagica.parent it would trace "Aaron" if you traced bluemagica.root it would trace EightySeven.
The parent however does no know of its grandchildren(children of ts children) unless you refer to the child before the grand child
EightySeven.bluemagica will throw and error
EightySeven.Aaron.bluemagica will not
in AS3 the parent knows how many children it has via the display list
if you traced Aaron.numChildren it would output 1, you ahve 1 child, bluemagica
if you traces Aaron.getChild(1) it would most likley trace bleumagica
there are many different ways to deal with children but I don't want to overwhelm you with them all atm
aaron_da_killa
02-28-2009, 12:14 AM
Whoa, thanks 87, that was quite alot of valuable insight. I have a few questions though.
var point:Point = new Point(a,b);
where as a and b are the local points of the object;
...new Point(enemy.x, enemy.y);
point.localToGlobal(point);
With that code, is that applied locally to the function? If I copied that to another part of my code would it work or would there be some sort of conflict? Because if I remember correctly, in c++ if you define a variable inside a function it can only be used inside that function and once you leave that function that variable is discarded. Is that the case in as3?
IMO your code is slightly confusing...
:o . Just kidding. I don't know how long I have been working in flash now, its probably reaching the one month mark. I come from a very limited c++ background in programming, I just knew very few things in c++ such as adding weapons and monsters and a few other things into Half-Life's source code, I'm certainly more on the art/design side at the moment. So I don't really know much about programming at all. I didn't really have any intention of learning flash until I received my multimedia major project assessment and I thought a flash game would have been really fun to make. Also a nice addition to my portfolio (I want to work in the industry ;) ). Turns out flash is pretty fun.
Anyway, now that the player is a standalone movieclip and my entire level is contained inside a movieclip named level2, it seems that for the most part my collision detection has broken. I am going to have to use localToGlobal in many more instances it seems. I don't know, it seems silly that you can't just say:
localToGlobal(level2.platform).y
or something similar to that.
Well, thanks so much for the help guys :D
EDIT:
Whats wrong with this code:
var platformPoint:Point = new Point(level2.platform.x, level2.platform.y);
platformPoint.localToGlobal(platformPoint);
I get this compile error:
"1061: Call to a possibly undefined method localToGlobal through a reference with static type flash.geom:Point."
bluemagica
02-28-2009, 01:31 AM
platformPoint is itself a point object, and the first portion needs to be a reference object whose relative local point will be changed to global!
oh and though i haven't tested it, but i think you can do level2.localToGlobal instead of level2.platform.parent.localToGlobal
aaron_da_killa
02-28-2009, 02:56 AM
Thanks mate.
platformPoint is itself a point object, and the first portion needs to be a reference object whose relative local point will be changed to global!
Do you think you could show me what its supposed to be because I think I did what you said but it didn't work.
aaron_da_killa
02-28-2009, 05:02 AM
Happy Double Post:
Ok, I figured it out. Here was my incorrect code:
var platformPoint:Point = new Point(level2.platform.x, level2.platform.y);
platformPoint.localToGlobal(platformPoint);
Here is my correct code:
var platformPoint:Point = new Point(level2.platform.x, level2.platform.y);
platformPoint.y = level2.localToGlobal(platformPoint).y;
platformPoint.x = level2.localToGlobal(platformPoint).x;
That said, I think I've worked out how localToGlobal works now. My new collison detection for platforms now looks like this:
public function platformSit():void{
var platformPoint:Point = new Point(level2.platform.x, level2.platform.y);
platformPoint.y = level2.localToGlobal(platformPoint).y;
platformPoint.x = level2.localToGlobal(platformPoint).x;
//TOP SIDE
if (player.y + player.height <= platformPoint.y + level2.platform.height/2 && player.x + player.width > platformPoint.x + 8 && player.x < platformPoint.x + level2.platform.width - 8){
playerJumping = false;
player.y = platformPoint.y - player.height;
}
//BOTTOM SIDE
else if (player.y >= platformPoint.y + level2.platform.height - level2.platform.height/2 && player.x + player.width > platformPoint.x + 8 && player.x < platformPoint.x + level2.platform.width - 8){
mainSpeedLeft = 4;
mainSpeedRight = 4;
jumpSpeed = 2;
}
//RIGHT SIDE
else if (player.x >= platformPoint.x + level2.platform.width - level2.platform.width/2 && player.y + player.height > platformPoint.y + 5){
mainSpeedLeft = 0;
playerTouchingPlatformSide = true;
}
//LEFT SIDE
else if ((player.x + player.width) <= (platformPoint.x + (level2.platform.width/2)) && player.y + player.height > platformPoint.y + 5){
mainSpeedRight = 0;
playerTouchingPlatformSide = true;
}
else {
playerTouchingPlatformSide = false;
mainSpeedLeft = 4;
mainSpeedRight = 4;
}
}
The only changes being the addition of lines 2, 3 and 4 and instead of referring to level2.platform's x or y coordinates I use platformPoint's x or y coordinates instead. I can still use level2.platform's height or width because this isn't effected at all by being inside another movieclip.
Thanks again bluemagica and EightySeven ;).
Edit:
oh and though i haven't tested it, but i think you can do level2.localToGlobal instead of level2.platform.parent.localToGlobal
Yup, I just tested it and it does indeed work!
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.