Hello, I'm new here and I'm fairly new to Flash actionscript, but I really want to know more. Anyhow, my issue here is that I have an object that is able to move around in 8 directions (North, Northwest, west, etc.) and accelerates. My problem is when it stops moving, the speed doesn't go back down to the base speed but instead stays at the max speed. Would anyone here be able to help me with this? On the stage is nothing but the object and the animation portions are just for the frames to look right when it is moving. My code is below:
- Code: Select all
onClipEvent (load)
{
basicSpeed = 3 //the speed I would like it to start at
addonSpeed = 0.25 //the rate at which it accelerates too:
maxSpeed = 12 //which is the top speed
var trueSpeed // this is the variable that is used to measure the movements
left = true //to say if character is facing left or right. for animation
var testSpeed = basicSpeed // basically setting up my behind the scenes variable to start at basicSpeed
var moving = false //to say whether or not the object is in movement
}
onClipEvent (enterFrame)
{
function routine() //this just wants to tell if the object is moving or not, and if not, start another function
{
if (moving = false)
{
slow_down()
}
}
routine ()
function accelerate() //to increase speed
{
if (testSpeed != maxSpeed)
{
testSpeed += addonSpeed;
}
trueSpeed = testSpeed;
}
function slow_down() //to slow down, but it doesn't look like it is working.
{
if (testSpeed > 3)
{
testSpeed -= addonSpeed
trueSpeed -= addonSpeed
}
}
if (Key.isDown (Key.RIGHT)) //rightwards movement
{
moving = true
if (left) //animation
{
left = false
this._xscale = -_xscale
}
accelerate() //movement
this._x += trueSpeed
}
if (Key.isDown (Key.LEFT))
{
moving = true
if (!left) //animation
{
left = true
this._xscale = -_xscale
}
accelerate() //movement
this._x -= trueSpeed
}
if (Key.isDown (Key.UP))
{
moving = true // movement
accelerate()
this._y -= trueSpeed
this.gotoAndStop(1) //animation
}
if (Key.isDown (Key.DOWN))
{
moving = true //movement
accelerate()
this._y += trueSpeed
this.gotoAndStop(2) //animation
}
}