PDA

View Full Version : Advice needed on cleaning up some script



Sapster
February 16th, 2009, 09:47 AM
Hi, I'm a complete beginner who ventured into the world of Flash and programming only a couple of days ago and need some advice on how to clean up some as2 script I've put together because I haven't been able to find tutorials on everything.

The script below does exactly what I need it to do at this stage for a one time project i.e.

1. Start an object moving in a certain direction on loading
2. Turn an object to face a certain direction and have the object move in that direction.
3. Make the movement perpetual (i.e. not reliant on continuous keystrokes or holding down a key) at a constant speed.

Apologies in advance if the following is offensive to anyone.


onClipEvent (load) {
upmove = 0.5

}
onClipEvent (enterFrame) {

if (Key.isDown(Key.RIGHT)) {
this.gotoAndStop(2);
rightmove = 0.5
leftmove = 0
upmove = 0
downmove = 0

}
else if (Key.isDown(Key.LEFT)) {
this.gotoAndStop(4);
leftmove = 0.5
rightmove = 0
upmove = 0
downmove = 0

}
if (Key.isDown(Key.UP)) {
this.gotoAndStop(1);
upmove = 0.5
downmove = 0
leftmove = 0
rightmove = 0

}
if (Key.isDown(Key.DOWN)) {
this.gotoAndStop(3);
downmove = 0.5
upmove = 0
leftmove = 0
rightmove = 0

}
speed = 0.5
_x += rightmove
_x -= leftmove
_y += downmove
_y -= upmove
}


My concern is that I arrived at point 3 above by banging my head relentlessly on the keyboard and employing an army of monkeys to sit and type random monkey-babble all day long, and no doubt there is a much prettier and more efficient way of achieving the same thing. Any advice on how I might do that would be much appreciated.

Cheers.


http://www.ezy-english.com

bluemagica
February 16th, 2009, 10:43 AM
you dont need the booleans


onClipEvent (load) {
speed=0.5;
xsp=0;
ysp=0;
}
onClipEvent (enterFrame) {

if (Key.isDown(Key.RIGHT)) {
this.gotoAndStop(2);
xsp=speed;
}
else if (Key.isDown(Key.LEFT)) {
this.gotoAndStop(4);
xsp=-speed;
}
if (Key.isDown(Key.UP)) {
this.gotoAndStop(1);
ysp=-speed;
}
else if (Key.isDown(Key.DOWN))
{
this.gotoAndStop(3);
ysp=speed;
}
this._x+=xsp;
this._y+=ysp;
}

Sapster
February 16th, 2009, 11:06 AM
Hey Blue,

Thanks for the reply. That's much easier on the eyes, but when I gave it a run, there was no initial movement, and when I change the object's direction it maintains some of the motion in the previous direction i.e. if it was initially moving upward and I change direction to the left, it will be facing left but moving upward and leftward simultaneously, instead of pure leftward motion.

Anything suggestions for modifying that script to include initial movement in any direction and eliminating any of the previous direction's motion?

Cheers.

Charleh
February 16th, 2009, 11:26 AM
onClipEvent (load) {
speed = 0.5;
xsp = 0;
ysp = -speed; // Set initial speed
}

onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
this.gotoAndStop(2);
xsp=speed;
}
else if (Key.isDown(Key.LEFT)) {
this.gotoAndStop(4);
xsp=-speed;
}
else {
// Stop horizontal movement if no LEFT/RIGHT key pressed
xsp = 0;
}

if (Key.isDown(Key.UP)) {
this.gotoAndStop(1);
ysp=-speed;
}
else if (Key.isDown(Key.DOWN))
{
this.gotoAndStop(3);
ysp=speed;
}
else {
// Stop verical movement if no UP/DOWN key pressed
ysp = 0;
}

this._x+=xsp;
this._y+=ysp;
}



Come to think of it, you want it to keep moving until another direction key is pressed - you know your original code is pretty close to perfect you just don't need 4 variables for the speed, only two!

I'm sure there are some full snake game tutorials on the net somewhere :)

Sapster
February 16th, 2009, 12:07 PM
Thanks Charleh,

Will need to sit and stare for a bit at what you guys have got there, then sleep on it. It'll hit me in the morning :)

Ah! I was trying to think of a game that used the type of movement I was trying to describe. Snake!

Cheers.

Charleh
February 16th, 2009, 01:11 PM
You could just use



onClipEvent (load) {
speed = 0.5;
dir = 1;
}

onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
this.gotoAndStop(2);
dir = 2;
}
else if (Key.isDown(Key.LEFT)) {
this.gotoAndStop(4);
dir = 0;
}

if (Key.isDown(Key.UP)) {
this.gotoAndStop(1);
dir = 1;
}
else if (Key.isDown(Key.DOWN))
{
this.gotoAndStop(3);
dir = 3;
}

switch(dir) {
case 0:
this._x -= speed;
break;
case 1:
this._y -= speed;
break;
case 2:
this._x += speed;
break;
case 3:
this._y += speed;
break;
}
}

Sapster
February 16th, 2009, 11:08 PM
Charleh,

Works great and easier for me to understand.

Much appreciated.