Ajay
Ajay

Reputation: 17

as3 Math.random() cant seem to get it working correctly

im working on as3 adobe flash, the FLA is a catching game and seems to work fine but i want to tweak it. i currently got :

trying to implement random speed per ball, i tried this:

var speed:Number = 7;
var RandomSpeed:Number = Math.random() * 7;
var ymov:Number =  RandomSpeed + speed;

and in the function i put this:

bgame[j].y += ymov;

(its [ j ] because i had to make another array to get the ball to drop)

its currently randomising all the balls in the game to the same speed but i want it to do it to individual balls.

there's also one more problem, when the game is finish (once player gets a score of 2 the game takes you back to home screen) the ball sprites that were on the screen and not caught still remain on the screen,

Upvotes: 0

Views: 133

Answers (2)

Aaron Beall
Aaron Beall

Reputation: 52133

You need to assign different ymov speeds to each ball. As it is now you assign the value at the top level scope, then use it to update each ball's position. This is why they are all the same speed.

You can assign a new random ymov property to each ball in your addBall() function:

bgame[i].ymov = 7 + Math.random() * 7;

Then in your Ballgame() update function move the ball based on that property:

bgame[j].y += bgame[j].ymov;

BTW as a style note, classes usually are UpperCase while variables and functions are lowerCase.

Upvotes: 1

CyanAngel
CyanAngel

Reputation: 1238

Your issue is that you are only "rolling the dice" once and using that result for the speed of every ball. Make ymov into a function it will produce a different result every time. IE:

function ymov():Number
{
    var speed:Number = 7;
    var RandomSpeed:Number = Math.random() * 7;
    return RandomSpeed + speed;
}

Upvotes: 0

Related Questions