ForumsProgramming Forumi need some help

8 4546
qaz34
offline
qaz34
10 posts
Nomad

I have a game i am making and there is one thing i cant find any where how to make a in game button change the speed var in my guy any help would be greatly apreciated

  • 8 Replies
master565
offline
master565
4,104 posts
Nomad

First, AS2 or AS3? Second, that's extremely basic, just make an onclick event and have the var equal the new speed. If your var was called speed, it will look like this

speed += (new speed);

Carlytoon
offline
Carlytoon
324 posts
Nomad

In AS3 a simple key press function looks like this:

/*We gonna make a listener that checks
a event (in this case a keyboard event) to run a function*/

stage.addEventListener(KeyboardEvent.KEY_DOWN,KeysDown);


//Now we gonna define the KeysDown function
function keysDown(e:KeyboardEvent):void{
//Check if the keyCode is pressed
if(e.keyCode=="keyCode number&quot{
speed="Your value";
}
//Optional, trace the keyCode of the key you press
trace(e.keyCode);
}

qaz34
offline
qaz34
10 posts
Nomad

hey guys im using as2 and im newish to the whole concept of as i may be writing it wrong but it didn't work the instance name of my character is player and the speed line is...
var speed:Number = 10;
if u could type the code u think it may be plz do it would help tones thx again

qaz34
offline
qaz34
10 posts
Nomad

bump

WhiskeyedJack
offline
WhiskeyedJack
80 posts
Shepherd

First off don't bump, bad manners and all. Second it would be easier to help if we could see more of the code you are trying to use.

var speed:Number = 10;

should work perfectly for setting the speed variable. You'll need to use _x or _y to move your movie clip across the screen. Ex:
var speed:Number = 10;
onEnterFrame() {
this._x -= speed;
}
qaz34
offline
qaz34
10 posts
Nomad

this is the code im using for my guy

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;
}
}

Trotiloff
offline
Trotiloff
1 posts
Peasant

hello everybody

arobegamr
offline
arobegamr
130 posts
Nomad

@Trotiloff You shouldn't post in this forum if you're not asking a question or helping, Armorgames has a Newcomers forum.

@qaz34 Your question isn't every clear. Why do you want to change your player's speed? Do you want the player to run?

Showing 1-8 of 8