# What is wrong with this code?

Discussion in 'Programming' started by veko, Jan 28, 2017.

Tags:
1. ### vekoGuest

I have this simple accelleration code on my obj enemy.
He is suppossed to dash towards player and the slow down.
In my create event it sets 1 to alarm[0] and in alarm[0] it sets direction towards player and speed on 50 and resets it self to 500.
And this is what i have on step event for slowing down:
if speed = 50
{
for (i = 0; i < 20; i += 1)
{
speed -= 2.5;
}
}
It seems that this code is not working since it follows the player on speed 50 forever.
If somebody could help me out that would be great. Thank you!

Joined:
Dec 19, 2016
Posts:
19
3. ### vekoGuest

But what?! Im sorry but i actually never had such a simple problem that i couldn't solve! I tried to go through the link you gave me but it seems that everything is in order.
I tried doing this but it doesn't change anything!
Code:
```if speed == 50
{
for (var i = 0; i < 20; i++;)
{
speed -= 2.5;
}
}```

4. ### SabnockMember

Joined:
Jul 21, 2016
Posts:
311
Take a look at the first line of code

5. ### SabnockMember

Joined:
Jul 21, 2016
Posts:
311
Also you are using a for to loop in the step so all you would see is the object stop not slow down over time.

6. ### Joe EllisMember

Joined:
Aug 30, 2016
Posts:
994
asking if its equal to 50 will only make the below code happen when its exactly 50, which it will only be for 1 frame then gradually reduce, so you need to ask if its more than 0, cus anything above 0 is still moving, also "for" loops are for making things happen more than once per frame, you need the object to only reduce speed once per frame, as you only see 1 frame at a time, so anything that happens in between frames will all join together and be displayed in the next frame

7. ### SalvakiyaMember

Joined:
Jul 17, 2016
Posts:
84
may or may not help... depends on your code... but look up friction in the help docs.

if speed>10{friction =.5}else{friction=.1}