The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.

ForumsProgramming Forumneed help with variables

14 5757
qaz34
offline
qaz34
10 posts
Nomad

im making a game and would like to know how to change my speed var in game with a button im using as2 my instance name is player and the code is


onClipEvent (load) {
var grav:Number = 0;
// gravity
var speed:Number = 10;
// how fast you walk
var jumpHeight:Number = 15;
// how high you jump
var slow:Number = .7;
// sets water falling speed
var slowspd:Number = speed/1.5;
// sets water walking speed
var setspeed:Number = speed;
var scale:Number = _xscale;
var ex:Number = 5;
// makes hitTests better, change for a closer hitTest (warning, more buggy if smalle, less real if further)
this.gotoAndStop(2);
}
onClipEvent (enterFrame) {
grav++;
_y += grav;
while (_root.ground.hitTest(_x, _y, true)) {
_y--;
grav = 0;
}
if (_root.water.hitTest(_x, _y, true)) {
if (grav>0) {
grav *= slow;
}
speed = slowspd;
} else {
speed = setspeed;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
_xscale = scale;
if (_root.ground.hitTest(_x, _y+3, true)) {
this.gotoAndStop(1);
} else {
this.gotoAndStop(2);
}
} else if (Key.isDown(Key.LEFT)) {
_x -= speed;
_xscale = -scale;
if (_root.ground.hitTest(_x, _y+3, true)) {
this.gotoAndStop(1);
} else {
this.gotoAndStop(2);
}
} else {
if (_root.ground.hitTest(_x, _y+3, true) && !Key.isDown(79) && !Key.isDown(73)) {
this.gotoAndStop(3);
}
}
if (Key.isDown(Key.UP) && !Key.isDown(87) && !Key.isDown(65) && !Key.isDown(68) && !Key.isDown(73)) {
this.gotoAndStop(5);
}
if (Key.isDown(Key.UP) && !Key.isDown(87) && !Key.isDown(65) && !Key.isDown(68) && !Key.isDown(79)) {
this.gotoAndStop(4);
}
if (Key.isDown(Key.UP) && _root.ground.hitTest(_x, _y+3, true)) {
grav = -jumpHeight;
_y -= 4;
this.gotoAndStop(2);
}
if (_root.ground.hitTest(_x+(_width/2)+ex, _y-(_height/2), true) || _root.ground.hitTest(_x+(_width/2)+ex, _y-(_height/6), true) || _root.ground.hitTest(_x+(_width/2)+ex, _y-_height, true)) {
_x -= speed;
}
if (_root.ground.hitTest(_x-(_width/2)-ex, _y-(_height/2), true) || _root.ground.hitTest(_x-(_width/2)-ex, _y-(_height/6), true) || _root.ground.hitTest(_x-(_width/2)-ex, _y-_height, true)) {
_x += speed;
}
if (_root.ground.hitTest(_x, _y-_height-15, true)) {
grav = 1;
}
}

any help will be appreciated

  • 14 Replies
ExplosionsHurt
offline
ExplosionsHurt
248 posts
Nomad

I don't know much about AS2, but it should go like this:

1. Make the button.
2. Right click it, and select "actions".
3. Type in:
on (release)
{
speed += 2;
}

Just replace 2 with how much speed you want on each click.

markzi
offline
markzi
103 posts
Nomad

But that would keep adding speed. I suggest you remove the button after you do that. Or you could go:
on(release){
speed = (number of speed you want)
trace("markzi is my hero )
// just in case
}

arobegamr
offline
arobegamr
130 posts
Nomad

I would suggest using two buttons, one to increase and one to decrease.
I dislike onClipEvents, as they make debugging more difficult, but I will give you that method.

(Where int = interval by which to modify the variable)
Up button:
on(release){
_root.speed -= int
}

Down button:
on(release){
_root.speed += int
}

qaz34
offline
qaz34
10 posts
Nomad

thanks for all of ur suggestions but they dident work D:

jorgeluis99
offline
jorgeluis99
1 posts
Nomad

xomo

Boomer404
offline
Boomer404
2 posts
Nomad

You should put this in your code:

on(release){
onClipEvent (load) {
var grav:Number = 0;
// gravity
var speed:Number = 10;
// how fast you walk
var jumpHeight:Number = 15;
// how high you jump
var slow:Number = .7;
// sets water falling speed
var slowspd:Number = speed/1.5;
// sets water walking speed
var setspeed:Number = speed;
var scale:Number = _xscale;
var ex:Number = 5;
// makes hitTests better, change for a closer hitTest (warning, more buggy if smalle, less real if further)
this.gotoAndStop(2);
}
onClipEvent (enterFrame) {
grav++;
_y += grav;
while (_root.ground.hitTest(_x, _y, true)) {
_y--;
grav = 0;
}
if (_root.water.hitTest(_x, _y, true)) {
if (grav>0) {
grav *= slow;
}
speed = slowspd;
} else {
speed = setspeed;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
_xscale = scale;
if (_root.ground.hitTest(_x, _y+3, true)) {
this.gotoAndStop(1);
} else {
this.gotoAndStop(2);
}
} else if (Key.isDown(Key.LEFT)) {
_x -= speed;
_xscale = -scale;
if (_root.ground.hitTest(_x, _y+3, true)) {
this.gotoAndStop(1);
} else {
this.gotoAndStop(2);
}
} else {
if (_root.ground.hitTest(_x, _y+3, true) && !Key.isDown(79) && !Key.isDown(73)) {
this.gotoAndStop(3);
}
}
if (Key.isDown(Key.UP) && !Key.isDown(87) && !Key.isDown(65) && !Key.isDown(68) && !Key.isDown(73)) {
this.gotoAndStop(5);
}
if (Key.isDown(Key.UP) && !Key.isDown(87) && !Key.isDown(65) && !Key.isDown(68) && !Key.isDown(79)) {
this.gotoAndStop(4);
}
if (Key.isDown(Key.UP) && _root.ground.hitTest(_x, _y+3, true)) {
grav = -jumpHeight;
_y -= 4;
this.gotoAndStop(2);
}
if (_root.ground.hitTest(_x+(_width/2)+ex, _y-(_height/2), true) || _root.ground.hitTest(_x+(_width/2)+ex, _y-(_height/6), true) || _root.ground.hitTest(_x+(_width/2)+ex, _y-_height, true)) {
_x -= speed;
}
if (_root.ground.hitTest(_x-(_width/2)-ex, _y-(_height/2), true) || _root.ground.hitTest(_x-(_width/2)-ex, _y-(_height/6), true) || _root.ground.hitTest(_x-(_width/2)-ex, _y-_height, true)) {
_x += speed;
}
if (_root.ground.hitTest(_x, _y-_height-15, true)) {
grav = 1;
}
}
}

WhiskeyedJack
offline
WhiskeyedJack
80 posts
Shepherd

Make sure you are incrementing "setspeed" & not "speed". You are constantly modifying the "speed" variable:

speed = slowspd;
} else {
speed = setspeed;

So if you have your button increment setspeed, then you will increase the max value that speed is set to each frame.
WhiskeyedJack
offline
WhiskeyedJack
80 posts
Shepherd

What I did is created two new symbols - specifically buttons - called speedUp and speedDown and placed them on the screen. I then provided with the instance names speedUpButton and speedDownButton respectively.

Then in your actions for the player you can insert the following:

_root.speedUpButton.onPress = function(){setspeed ++;}
_root.speedDownButton.onPress = function(){setspeed --;}


Hope that helps.
qaz34
offline
qaz34
10 posts
Nomad

where would i put ]
_root.speedUpButton.onPress = function(){setspeed ++;}

WhiskeyedJack
offline
WhiskeyedJack
80 posts
Shepherd

You should be able to stick it in the actions for the player:

onClipEvent (enterFrame) {
_root.speedUpButton.onPress = function(){setspeed ++;}
grav++;
_y += grav;
qaz34
offline
qaz34
10 posts
Nomad

OMGGGGGG IT WORKED THXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

qaz34
offline
qaz34
10 posts
Nomad

now will] it work if
_root.jumpUpButton.onPress = function(){setjumpHeight += 5;}
to change the jumpHeight ?

arobegamr
offline
arobegamr
130 posts
Nomad

No offense, but did you copy this code?

Never attempt to use snippits of code in a game. Snippits should be for learning purposes only, or you will never truly understand the language.

Imagine only being able to speak in sentences someone else wrote for you

qaz34
offline
qaz34
10 posts
Nomad

yes i did copy i only started lately and is trying to learn the language but don't know where to start so i went online and found codes for a game and thought i would start there
if u could tell me a place to start i would be thankful

Showing 1-14 of 14