00:00
00:00
Newgrounds Background Image Theme

Iva0413 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS2 help needed 2014-12-18 18:06:32


Ok so i'm having trouble with something that should be fairly easy to fix but i dont know enough about actionscript to figure
out what i'm doing wrong.
Ok so lets say i have an object on stage and i want it to move left and right when it hits a certain point how should i go about it?

i tried this but it contradicts itself.

onClipEvent(load){

var speed = 1;
}

onClipEvent (enterFrame){
if (this._x <250){
this._x=-speed;

}else{
if (this._x >500){
this._x=+speed;

this obviously doesn't work because i'm telling the object to move in both directions between these values.
So what i want to do is have the object looping from left to right between two values. it should be a fairly straight forward fix
but i'm just not clever enough to figure it out.

Can anyone help?

Response to AS2 help needed 2014-12-18 18:59:11


Use a boolean to track which direction the object should be moving.

So, for example, you could call it doMoveRight and only increment the object's x-coordinate if the boolean is true, and decrement it if the boolean is false; then when the object passes 500 on the x-axis and the boolean is true set the boolean to false, and when it passes 250 on the x-axis and the boolean is false set it to true. Just make sure to check the value of both the x-coordinate and the boolean in your if-statements.

Response to AS2 help needed 2014-12-18 19:22:45


hey thanks for the response. I tried using the Booleans before but im not sure if i was going about it the right way. Like you said i set it to true when moveleft and right. so it was something like this.

onClipEvent(load){
var speed = 1;
var walkleft:Boolean = false;
var walkright:Boolean = false;

}
onClipEvent (enterFrame){
if (this._x <250){
this._x=-speed;
walkleft = true;
walkright = false;

}else{
if (this._x >500){
this._x=+speed;
walkright = true;
walkleft = false;

now i know this code here doesnt work because im going the wrong way about it but i also tried adding the walkleft to the
main statement like.

onClipEvent (enterFrame){
if (this._x <250) && !walkleft){ ////////this bit here is wrong.
this._x=-speed;
walkleft = true;
walkright = false;

}else{so and so...

Response to AS2 help needed 2014-12-18 19:40:09 (edited 2014-12-18 19:41:32)


For what you're doing you don't need two booleans; one will suffice. I also kinda messed up my suggestion when I said to put the checks into one if-statement, sorry.

A simpler solution would be to just use an if-else-statement and check the value of the boolean, and increment the x-coordinate on true and decrement it on false. Inside each statement check if the object has exceeded the necessary bounds and change the boolean accordingly. That might sound more complicated than it actually is:

onClipEvent(load){
  var speed = 1;
  var doMoveRight = true;
}

onClipEvent (enterFrame){
  if (doMoveRight) {
    this._x += speed;
    doMoveRight = (this._x < 500);
  } else {
    this._x -= speed;
    doMoveRight = (this._x < 250);
  }
}

Depending on where you have your object's registration point, the "bouncing" might be a little bit off (you'll see what I mean if you run the code). By default the registration point is at the top left, so if you want to fix that "improper bounce" (I don't know how else to describe it) you need to check if, when moving rightward, the object's x-coordinate exceeds 500 minus the object's width:

onClipEvent(load){
  var speed = 1;
  var doMoveRight = true;
}

onClipEvent (enterFrame){
  if (doMoveRight) {
    this._x += speed;
    doMoveRight = (this._x < (500-this._width));
  } else {
    this._x -= speed;
    doMoveRight = (this._x < 250);
  }
}

That should do what you need it to.

Response to AS2 help needed 2014-12-18 19:56:10


thanks a mill for this. I will give it a go and see how it works out. I should add that this could be done using setIntervals
which might remove bouncing issues. so another way to go could be this. would this work or cause more problems?

walkleft = setInterval (walkleft,30)
var speed:Number = 1;

function walkleft(){
this.gotAndStop(2);
this._x = this._x - speed;
this._xscale =+100;

if(this._x <=200){

clearInterval (walkleft);
walkright();
walkright = setInterval (walkright,30);

}
}

function walkright(){
this.gotoAndStop(2);
this._x = this._x - speed;
this._xscale =-100;

if(this._x >=600){

clearInterval (walkleft);
walkright();
walkleft = setInterval (walkleft,30);

}
}

thanks again.

Response to AS2 help needed 2014-12-18 20:21:11


At 12/18/14 07:56 PM, jaypeps wrote: I should add that this could be done using setIntervals which might remove bouncing issues.

No, that won't help.

The reason you'll get the behaviour I described is because the x-coordinate is based on where the registration point is. So if it is set to top-left, which it is by default, that means the x-coordinate is wherever the top-left of the object is, which means that the object will only start moving leftward when the left side of it passes the 500 x-coordinate.

So using an interval won't prevent that from happening; you need to change the point on the x-axis that is considered the bounds (i.e. the point on the x-axis minus the width of the object).

At 12/18/14 07:56 PM, jaypeps wrote: so another way to go could be this. would this work or cause more problems?

No, that won't work.

You've created two functions and given them the names walkleft and walkright, and then you assign to those names the return value of setInterval(), which returns an integer. So you're attempting to assign an integer to a function, which will produce a type mismatch error.

If you were to remove the assignment of setInterval()'s return value it would work, yes, but you would still get "bouncy" behaviour that I described above. However, don't use setInterval for this; it just over-complicates things.

Response to AS2 help needed 2014-12-18 20:40:50


I agree it seems to be a lot more complicated a code for what i want it to do. I think i read somewhere that as2 is bad for clearing intervals anyway so i will give your code a go. So just to clear things up and im sorry for asking too many questions
here. if say my registration point is set to the center of the object and the objects x value is 100 then its center should sit at the 500 stage value and that will work? either way the registration point is not all that important for what i want to do because
i can put this object anywhere on stage at the mo. i am curious about this "bouncy".

Thanks again for your help :)

Response to AS2 help needed 2014-12-18 20:53:18


At 12/18/14 08:40 PM, jaypeps wrote: if say my registration point is set to the center of the object and the objects x value is 100 then its center should sit at the 500 stage value and that will work?

If the registration point is set to the middle and the object's x-coordinate is at 100 then the middle of the object will be at 100 on the x-axis, and to eliminate the effect I described you would need to do 500 minus half of the object's width (since the x-coordinate will be halfway from the right side), and the other bound would need to be 250 plus half of the object's width.

At 12/18/14 08:40 PM, jaypeps wrote: either way the registration point is not all that important for what i want to do because i can put this object anywhere on stage at the mo.

The registration point doesn't affect where you can and cannot place the object; it just changes where the coordinates point to.

At 12/18/14 08:40 PM, jaypeps wrote: i am curious about this "bouncy".

View this example to see what I'm talking about. The box on the top has the effect I described, and the bottom one is accommodating for the fact that the registration point is set to the top-left; the top is what you don't want to happen, the bottom is what you do want.

Response to AS2 help needed 2014-12-18 21:22:32


ahh yes i see what you mean with the bouncy thing. its just registering in accordance to the movieclip registration point so yes indeed subtracting and adding half the width of the movieclip from the stage value makes sense. so in this case the registration point makes all the difference my bad.

thanks a mill Diki im sure this code will do the job great :)

onClipEvent(load){
var speed = 1;
var doMoveRight = true;
}

onClipEvent (enterFrame){
if (doMoveRight) {
this._x += speed;
doMoveRight = (this._x < (500-this._width));
} else {
this._x -= speed;
doMoveRight = (this._x < 250);
}
}