PDA

View Full Version : Help With Borders.


Zend
02-27-2009, 02:57 PM
So basically I was wondering if there's any way to make it so the object your controlling isn't aloud to move off the screen without physically making borders. Very simple question :)

There's also a copy of what I'm working on attached to this thread if you need more help understanding what I'm trying to do.

Umbrascitor
02-27-2009, 03:18 PM
All you need is a simple if { block in your key handlers. In pseudo-code:

function goleft() {
if (guy.xposition > left limit) {
execute movement
}
}

Function godown() {
if (guy.yposition < down limit) {
execute movement
}
}

... and so on.

If speed is a factor, then you can change it so that "if {" the character moves beyond the limit, then set its coordinate position at the limit.

Hope that helps.

Zend
02-27-2009, 03:28 PM
All you need is a simple if { block in your key handlers. In pseudo-code:

function goleft() {
if (guy.xposition > left limit) {
execute movement
}
}

Function godown() {
if (guy.yposition < down limit) {
execute movement
}
}

... and so on.

If speed is a factor, then you can change it so that "if {" the character moves beyond the limit, then set its coordinate position at the limit.

Hope that helps.

I'm kinda confused. So this function is saying:
If the guys x position is greater then "left limit" then "execute movement"?

So does this mean I'll have to make a variable for "left limit" and a function for "execute movement"? If so then I think I understand what your saying.

[Edit]
xposition isn't even a real command :\
So does this mean I'll have to make a variable called xposition and one called yposition for this to work too?

Isn't there an easier way?

bluemagica
02-27-2009, 04:09 PM
seriously, read the thing first.....he clearly said it is "pseudo code"......meaning not real code, but the actual logic behind it written in a code like structure!

Zend
02-27-2009, 11:09 PM
seriously, read the thing first.....he clearly said it is "pseudo code"......meaning not real code, but the actual logic behind it written in a code like structure!

Sorry I'm new to the site and I don't know all these terms yet.

Could someone please explain how to make the variables yposition and xposition?

var xposition:Number = 0;

How would I make it so that the number changes to the x position of guy_mc?

[Edit]
Also, is there a command I can use to make it so the characters position resets when I enter a new frame? Because right now when I hit the finish he just stays in the same place when it switches to the next frame, making him instantly beat every level :(

I just want to do something simple like:
On ENTER_FRAME guy_mc.x will equal a specific number.
But how do I do that so it only does it once when I enter the frame instead of keep doing it even after the level has started?

aaron_da_killa
02-28-2009, 01:09 AM
To reposition something along a given axis you use:

myAwesomeMovieClip.y = 0;

This code moves a movie clip with the instance name myAwesomeMovieClip to the top of the stage.

As for making the player not fall of the screen simple! Let me show you some ways to do it.

If this is how I move my player:


if(leftKeyDown){
player.x -= mainSpeedLeft;
}


than I could add:


if(leftKeyDown && player.x > 0){
player.x -= mainSpeedLeft;
}


Or I could do this:


if(leftKeyDown){
if (player > 0){
player.x -= mainSpeedLeft;
}
else{
player.x -= 0;
}

}


Basically that says if the player is one pixel or more away from the left edge of the stage, than decrement the player's x position by mainSpeedLeft (which is 4, meaning the player will move 4 pixels to the left every frame). Otherwise (if the player is not more than one pixel away from the edge) don't move the player at all. This can also be written as:


if(leftKeyDown){
if (player > 0){
player.x -= mainSpeedLeft;
}
}


Ofcourse, this is only for the left edge, you need to do something similar for the other 3 edges.

Zend
03-02-2009, 02:16 AM
Omg thank you so much, that was very user friendly. Got my border all done :)

Heres what I did to reset the guys (guy_mc) position when he passes to the new level:


//---Event Listeners---\\

addEventListener(Event.ENTER_FRAME, positionReset);

//---Functions---\\

function positionReset(event:Event):void{
guy_mc.x = 275
guy_mc.y = 395
}


But because the event is ENTER_FRAME it wont allow me to move because his position is constantly reseting. Is there any other event that will only do it one time?

bluemagica
03-02-2009, 03:03 AM
make a custom function or use a variable like

var current_level = 7 //assume
var last_level = 6;

enterframe()
{
if(last_level<current_level)
{
positionReset();
last_level++;
}
}

Zend
03-02-2009, 03:12 AM
make a custom function or use a variable like

var current_level = 7 //assume
var last_level = 6;

enterframe()
{
if(last_level<current_level)
{
positionReset();
last_level++;
}
}

Won't the last level always be less then the current level though? It seems like that would only do the same thing as the ENTER_FRAME event will do.

bluemagica
03-02-2009, 03:23 AM
last_level++ means its now 7, so the if condition wont be executed again untill current_level is 8

Zend
03-02-2009, 03:35 AM
last_level++ means its now 7, so the if condition wont be executed again untill current_level is 8

Thank you sir, it worked wonders :)