randomize Nested symbols for coin blocks - actionscript-3

I have a MovieClip object called coinblock1.
Problem:
Coinblock1 has 6 frames and I want to choose a random frame number from this MC object.
I want to use this number to later update some other variables by using an IF statement.
Variables to update include the changing of amount of coins earned by the player.
My frame numbers are arranged like this:
frame 1 = 0 coins
frame 2 = 1 coins
frame 3 = 5 coins
frame 4 = 10 coins
frame 5 = 50 coins
frame 6 = 100 coins
How to randomize the range for following frames in the coin block?
I have used the if...else statement and gotoAndStop in a currentFrame.
My Code:
if(coinblock1.hitTestObject(blowfishPong) == true)
{
if (ballSpeedXCoinGame > 0)
{
ballSpeedXCoinGame *= -1;
ballSpeedYCoinGame = calculateBallAngleCoinGame(character.y, blowfishPong.y);
var random1: uint = 0;
random1 = Math.random() * coinblock1.totalFrames;
if (random == 1){
coinblock1.gotoAndStop(1);
Bump.play();
}
else{
if(random == 2){
Coins += 1;
coinblock1.gotoAndStop(2);
}
if(random == 3){
Coins += 5;
coinblock1.gotoAndStop(3);
}
if(random == 4){
Coins += 10;
coinblock1.gotoAndStop(4);
}
if(random == 5){
Coins += 50;
coinblock1.gotoAndStop(5);
}
if(random == 6){
Coins += 100;
coinblock1.gotoAndStop(6);
}
updateTextFieldsCoinGame();
CoinSFX.play();
}
}
}

My understanding is you want to get a random number and then use it in an IF statement to control which frame the coinblock1 goes to and also to update the coins amount.
For that, try something like this logic below:
if(coinblock1.hitTestObject(blowfishPong) == true)
{
if (ballSpeedXCoinGame > 0)
{
ballSpeedXCoinGame *= -1;
ballSpeedYCoinGame = calculateBallAngleCoinGame(character.y, blowfishPong.y);
//# ::: Step 1 ::: first you define a variable (it has value of zero)
var random1: uint = 0;
//# then you update the variable (with some random value)
random1 = ( Math.random() * coinblock1.totalFrames );
//# finally you check what the new updated value is now
trace( "random1 is : " + random1 );
//# ::: Step 2 ::: update coins amount and then change coinblock's frame number
if( random1 == 1 ) //# will do this part if random1 is 1..
{
coinblock1.gotoAndStop(1);
Bump.play();
}
else //# will do this part if random1 is NOT 1..
{
if( random1 == 2 )
{
Coins += 1;
coinblock1.gotoAndStop(2);
}
if( random1 == 3 )
{
Coins += 5;
coinblock1.gotoAndStop(3);
}
if( random1 == 4 )
{
Coins += 10;
coinblock1.gotoAndStop(4);
}
if( random1 == 5 )
{
Coins += 50;
coinblock1.gotoAndStop(5);
}
if( random1 == 6 )
{
Coins += 100;
coinblock1.gotoAndStop(6);
}
//# after the IF's... now update the in-game text and also play a sound
updateTextFieldsCoinGame();
CoinSFX.play();
} //# end of ELSE part
}
}

Related

Button generating a cross sum

I'm looking for a tip for a AS3 script, have no idea how to start there
Button, if clicked the function is executed, which outputs a predefined value as the cross sum of a number string.
Example:
Cross sum should be 10
By clicking on the button, the function generates the number 55 or 82 or 37 or 523, ie numbers with the cross sum 10
An alternative way using % (modulo) instead of a string. You could write that into one line like this:
while (sum != 0) { qsum += sum % 10; sum /= 10; }
The trick is that modulo will give us only the last digit of the longer number, then we divide by 10 to trim off that last number (from longer) and we re-read a newer ending digit of the long number.
Example:
Long num = 1234, so each trim gives, 4 then 3 then 2 then 1 and we'll sum them up each time.
usage:
myInt = cross_sum(50); //makes myInt hold answer result of function (where ExpectedValue input is 50).
and the supporting function...
function cross_sum( ExpectedValue: int ) : int
{
var rand :int = Math.floor(Math.random() * 100000000000)
var sum :int = Math.abs( rand );
var qsum :int = 0;
while (sum != 0)
{
qsum += sum % 10; //get last digit of sum...
sum /= 10; //trim down sum by 1 digit...
}
if ( qsum == ExpectedValue ) { return rand; } //# stop here and give back "rand" as answer result.
else { cross_sum( expectedValue ); } //# else if wrong, try again...
}
Got it now.....
the function calculates a number, with the crosssum 50
function berechnen() {
var rand = Math.floor(Math.random() * 100000000000)
var sum = String(rand)
var qsum = 0;
for (var i = 0; i < sum.length; i++) {
qsum += Number(sum.charAt(i));
}
if (qsum == 50) {
summe.text = String(sum);
} else {
berechnen()
}
}

Random generate three integer values and check if they are unique

First, I am generating random integer values to get some values to check.
var A1, B2, C3:int;
A1 = Math.random() * 100 + 1;
B2 = Math.random() * 100 + 1;
C3 = Math.random() * 100 + 1;
Then I want to check if all the variables are unique from each other.
if (!(A1 == B2 || A1 == C3 || B2 == C3)){
unique = true;
}else{ // Not unique
}
If the variables are not unique to each other, I want to keep only the value for A1, and then change the two other variables B2 and C3 and then again check if they are unique.
}else{ // Not unique
if (unique = false){
do{
B2 = Math.random() * A1 + 1;
C3 = Math.random() * A1 + 1;
if (!(A1 == B2 || A1 == C3 || B2 == C3)){
unique = true;
}while (unique = true)
}
trace("Not unique");
}
My problem is that I cannot get three unique values, and any help on how I can solve this is highly appreciated.
This is a case where instead of asking to resolve the problem at hand you should probably have started by explaining what you are trying to do and what's the best way to achieve it. Since you have been posting a few times already my guess is that you are trying to extract 3 unique int from a range of 0 to 100. If that's the case then you are trying to create a complex and inefficient way to do it which is interesting by itself but is far from the point. The solution is to simply generate an array of int from 0 to 100 and then extract 3 of them randomly. In that case they will always be unique and always from 0 to 100.
var intRange:Array = [];
for(var i:int = 0; i < 100; i++)
{
intRange.push(i);
}
var index:int = Math.flor(Math.random() * intRange.length);
var a:int = intRange[index];
intRange.splice(index, 1);
index = Math.flor(Math.random() * intRange.length);
var b:int = intRange[index];
intRange.splice(index, 1);
index = Math.flor(Math.random() * intRange.length);
var c:int = intRange[index];
intRange.splice(index, 1);
//3 unique int from 0 to 100 range
Your problem is the wrong code here:
if (unique = false) { // must be: if (unique == false)
do {
...
} while (unique = true) // must be: while(unique == false)
}
also it can be reduced to
while(!unique) {
...
}
Also, there you are taking random from A1, not 100. Just too many errors. The proper code should be:
var a:int, b:int, c:int; // also, why would you call variables A1, B2, C3???
a = Math.random() * 100 + 1;
do {
b = Math.random() * 100 + 1;
while(b == a);
do {
c = Math.random() * 100 + 1;
} while(c == a || c == b);
// here you have unique
Now, the answer given by BotMaster is correct and robust but it involves filling an array and splicing it which is not always ok, especially if random range is significantly more than 100 and we need only three values from it.

AS3-Flash cs6 How to make numbers have a comma?

I am making a game that when you click on the Monster your score gets +1. But when your score goes over 1000 I would like it like this 1,000 rather than 1000. I am not sure how to do this as I have not learnt much action script. I have embed number and punctuation into the font. Here is my code so far:
var score:Number = 0;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
Monster.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler);
function fl_TapHandler(event:TouchEvent):void
{
score = score + 1;
Taps_txt.text = (score).toString();
}
Help will greatly appreciated.
You can do like that:
function affScore(n:Number, d:int):String {
return n.toFixed(d).replace(/(\d)(?=(\d{3})+\b)/g,'$1,');
}
trace(affScore(12345678, 0)); // 12,345,678
This may not be the most elegant approach, but I wrote a function that will return the string formatted with commas;
public function formatNum(str:String):String {
var strArray:Array = str.split("");
if (strArray.length >= 4) {
var count:uint = 0;
for (var i:uint = strArray.length; i > 0; i--) {
if (count == 3) {
strArray.splice(i, 0, ",");
count = 0;
}
count++;
}
return strArray.join("");
}
else {
return str;
}
}
I tested it on some pretty large numbers, and it seems to work just fine. There's no upper limit on the size of the number, so;
trace (formatNum("10000000000000000000"));
Will output:
10,000,000,000,000,000,000
So in your example, you could use it thusly;
Taps_txt.text = formatNum(String(score));
(This is casting the type implicitly rather than explicitly using toString();, but either method is fine. Casting just looks a little neater in function calls)
Use the NumberFormatter class:
import flash.globalization.NumberFormatter;
var nf:NumberFormatter = new NumberFormatter("en_US");
var numberString:String = nf.formatNumber(1234567.89);
trace("Formatted Number:" + numberString);
// Formatted Number:1,234,567.89
To show the score with comma, you can do like this : ( comments are inside the code )
var score:Number = 0;
var score_str:String;
var score_str_len:int;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
Monster.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler);
function fl_TapHandler(event:TouchEvent):void
{
score = score + 1;
score_str = score.toString();
score_str_len = score_str.length;
// here you can use score > 999 instead of score_str_len > 3
Taps_txt.text =
score_str_len > 3
// example : 1780
// score_str_len = 4
// score_str.substr(0, 4 - 3) = 1 : get thousands
// score_str.substr(4 - 3) = 780 : get the rest of our number : hundreds, tens and units
// => 1 + ',' + 780 = 1,780 : concatenate thousands + comma + (hundreds, tens and units)
? score_str.substr(0, score_str_len-3) + ',' + score_str.substr(score_str_len-3)
: score_str
;
// gives :
// score == 300 => 300
// score == 1285 => 1,285
// score == 87903 => 87,903
}
EDIT :
To support numbers greater than 999.999, you can do like this :
function fl_TapHandler(event:MouseEvent):void
{
score = score + 1;
score_str = score.toString();
Taps_txt.text = add_commas(score_str);
}
function add_commas(nb_str:String):String {
var tmp_str:String = '';
nb_str = nb_str.split('').reverse().join('');
for(var i = 0; i < nb_str.length; i++){
if(i > 0 && i % 3 == 0) tmp_str += ',';
tmp_str += nb_str.charAt(i);
}
return tmp_str.split('').reverse().join('');
/*
gives :
1234 => 1,234
12345 => 12,345
123456 => 123,456
1234567 => 1,234,567
12345678 => 12,345,678
123456789 => 123,456,789
1234567890 => 1,234,567,890
*/
}
Hope that can help you.

ActionScript 3.0 TypeError #1009 Null object? but there is no null object?

I am making a game in flash and my only problem so far is this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at farmgame227_fla::MainTimeline/onenter()[farmgame227_fla.MainTimeline::frame2:144]
The line will vary depending on how I move to the next frame. I am 99% sure a null object is not the problem, so I am stumped. I will show my entire code for frame 2 and I would appreciate it so much if anyone could lend some insight.
stop();
import flash.events.Event;
addEventListener(Event.ENTER_FRAME,onenter);
stop();
//starting positions for sheep
sheep1.y = 330
sheep1.x = 350
sheep2.y = 360
sheep2.x = 380
sheep3.y = 300
sheep3.x = 450
sheep4.y = 330
sheep4.x = 350
gold.y = 150
gold.x = 800
goat.y = 620
goat.x = 800
var myscore = 0;
function onenter(e:Event)
{
//hand follows mouse cursor
hand.x = mouseX;
hand.y = mouseY;
//sheep velocity
sheep1.x = sheep1.x + 1;
sheep1.y = sheep1.y - 1;
sheep2.x = sheep2.x - 1;
sheep2.y = sheep2.y - 1;
sheep3.x = sheep3.x + 1;
sheep3.y = sheep3.y + 1;
sheep4.x = sheep4.x - 3;
sheep4.y = sheep4.y + 3;
//sheep1's boundaries
if (sheep1.y < 0 || sheep1.y > 775 || sheep1.x < 0 || sheep1.x > 775)
{
removeEventListener(Event.ENTER_FRAME,onenter);
nextFrame();
}
//sheep2's boundaries
if (sheep2.y < 0 || sheep2.y > 775 || sheep2.x < 0 || sheep2.x > 775)
{
removeEventListener(Event.ENTER_FRAME,onenter);
nextFrame();
}
//sheep3's boundaries
if (sheep3.y < 0 || sheep3.y > 775 || sheep3.x < 0 || sheep3.x > 775)
{
removeEventListener(Event.ENTER_FRAME,onenter);
nextFrame();
}
//sheep4's boundaries
if(sheep4==null)
{
trace("4 it's null");
}
if (sheep4.y < 0 || sheep4.y > 775 || sheep4.x < 0 || sheep4.x > 775)
{
removeEventListener(Event.ENTER_FRAME,onenter);
nextFrame();
}
//hand hitting sheep, sending them back
if(sheep1==null)
{
trace("1 is null");
}
if(hand==null)
{
trace("hand is null");
}
if(fence==null)
{
trace("fence is null");
}
if (hand.hitTestObject (sheep1))
{
sheep1.y = 330;
sheep1.x = 350;
myscore = myscore + 1;
}
if (hand.hitTestObject (sheep2))
{
sheep2.y = 360;
sheep2.x = 380;
myscore = myscore + 1;
}
if (hand.hitTestObject (sheep3))
{
sheep3.y = 300;
sheep3.x = 450;
myscore = myscore + 1;
}
if (hand.hitTestObject (sheep4))
{
sheep4.y = 330;
sheep4.x = 350;
myscore = myscore + 1;
}
if (hand.hitTestObject (gold))
{
gold.x = 2500;
gold.y = 610;
gold.x = gold.x - 9;
myscore = myscore + 5;
}
if (hand.hitTestObject (goat))
{
removeEventListener(Event.ENTER_FRAME,onenter);
nextFrame();
}
//hand touching fence, results in game over
if (hand.hitTestObject (fence))
{
removeEventListener(Event.ENTER_FRAME,onenter);
nextFrame();
}
score.text = myscore;
//changing sheep speed based on score
//score 40 and 55 should be next
if (myscore >= 12)
{
sheep2.x = sheep2.x - 2;
sheep2.y = sheep2.y - 2;
}
if (myscore >= 24)
{
sheep3.x = sheep3.x + 2;
sheep3.y = sheep3.y + 2;
}
if (myscore >= 30)
{
sheep1.x = sheep1.x + 3;
sheep1.y = sheep1.y - 3;
}
//initiating and controlling gold and goat
if (myscore > 17)
{
gold.x = gold.x - 8;
}
if (gold.x < -9)
{
gold.x = 2000;
gold.y = 610;
gold.x = gold.x - 9;
}
if (myscore > 8)
{
goat.x = goat.x - 3;
}
if (goat.x < -9)
{
goat.x = 1600;
goat.y = 150;
goat.x = goat.x - 4;
}
}
You should shift all the checks in onenter upwards, prior to assigning anything to either sheep or other objects. Then you will likely find that one of the objects is null. The #1009 error means that some object reference that's placed right before a "." qualifier is null, and you can't get properties out of nothing.
Also, try placing return right after any call of nextFrame(), because when you perform nextframe, the previous frame's vars are invalidated, thus you can't refer to say hand because there's no more hand, it's destroyed along with the whole previous frame.

increase chances of even number

If I am getting a random number, how do I increase my chances of making that random number to be even. I am not looking to make it even every time. I am just looking to generate a random number say... %70 of the time or %90 of the time.
private function randNum (high, low) {
return Math.floor(Math.random() * (1+high-low)) + low;
}
Would I pass in a greater range of numbers to this function? Or would I have to scrap this function altogether?
Thank you
private function randNum (high : Number, low : Number) : int
{
var even : Boolean = Math.random() < 0.7; //set probability of even number here
var rand : int = Math.floor(Math.random() * (1+high-low)) + low;
if (even)
while (rand % 2 != 0)
rand = Math.floor(Math.random() * (1+high-low)) + low;
else
while (rand % 2 != 1)
rand = Math.floor(Math.random() * (1+high-low)) + low;
return rand;
}
Test:
var even : int = 0;
var odd : int = 0;
for (var i : int = 0; i < 100000; i++)
{
var a : int = randNum(1, 20);
if (a % 2 == 0)
even++;
else
odd++;
}
trace(even, odd);
Output:
[trace] 69869 30131
A little too late ;) but another one with no loop and using bit masking operation :
ret & -2 will make your number even, then depending on the result of (Math.random() >= evenProbability) you set the lower bit to be 1 to give an odd number
function randomRange(low:int, high:int, evenProbability:Number = 0.5):int{
var ret:int = int( Math.random() * ( 1 + high - low ) ) + low
return ( ret & -2 ) | int( Math.random() >= evenProbability )
}
Here a live test with wonderfl : http://wonderfl.net/c/9IHx