Chance button in flash AS 3.0 - actionscript-3

i would like to ask this community for some help with my end-course project. I am making a tell tale game where if you click a button there is a chance of you moving from frame 1 to frame 2 or there is a chance of lets say 30% of going to slide 3. This is the script i am using right now.
stop();
Button1_btn.addEventListener(MouseEvent.CLICK, Shoot_1);
function Shoot_1 (event:MouseEvent):void {
gotoAndPlay(2);
}
I apologize for the poor structure of this post, this is my very first post.
PS: My goal is to make it go and play frame 2 at a % chance or go and play frame 3 at a % chance.
Thank you for your help

My suggestion would be use random number to check if it's greater than 7 because you want 30% of chance to get to frame 3.
stop();
var result:uint
Button1_btn.addEventListener(MouseEvent.CLICK, Shoot_1);
function Shoot_1 (event:MouseEvent) :void
{
result = randomIntBetween(1, 10);
if(result > 7)
{
gotoAndPlay(3);
}
else
{
gotoAndPlay(2);
}
}
var percentage:String = result + '0%';
trace(percentage);
function randomIntBetween(min:int, max:int):int {
return Math.round(Math.random() * (max - min) + min);
}

Related

Collision event twice as3

I have a brick clip that goes to frame 2 when hit by a ball clip. This code is inside the brick class, which is why why it is referred as "this":
if (this.hitTestObject(_root.mcBall)){
_root.ballYSpeed *= -1;
this.gotoAndStop(2);
}
My question is when it is hit the second time how can it go to frame 3? What code do I need to add?
Try a "clean" approach, like this:
if (this.hitTestObject(_root.mcBall)){
_root.ballYSpeed *= -1;
if (this.currentFrame !== 3) {
this.nextFrame();
}
}
This makes the clip go to its next frame if its current frame isn't 3.
You can verify the current frame of your brick and then if it's frame 2 go to frame 3, like this :
if (this.currentFrame === 2){
this.gotoAndStop(3)
}
You can also use a boolean to indicate if your brick has been hit. If true, go to frame 3.
EDIT
AS code :
- Using a boolean :
...
var hit:Boolean = false
...
if (this.hitTestObject(_root.mcBall)){
_root.ballYSpeed *= -1
if(!hit){ // this is the 1st time so set hit to true and go to frame 2
hit = true
this.gotoAndStop(2)
} else { // this is the 2nd time so go to frame 3
this.gotoAndStop(3)
}
}
- Using currentFrame :
if (this.hitTestObject(_root.mcBall)){
_root.ballYSpeed *= -1
if (this.currentFrame == 1){ // we are in the 1st frame so go to frame 2
this.gotoAndStop(2)
} else { // we are certainly not in the 1st frame so go to frame 3
this.gotoAndStop(3)
}
}
I hope that is more clearer.

Flash Actionscript 3 Simple Counter

I am struggling to make a simple flash actionscript 3 counter in a text field, that starts at the beginning of my animation at 0, and increases with each frame.
This will count up until it reaches a particular frame on the timeline, where I would like the number to begin doubling instead of counting up.
I can only seem to find AS2 examples, and i'm not too sure how to get the double part working. Can anyone out there help me?
Thanks in advance!
I'm guessing you are using Flash CS and you are putting your code directly on the frames, so this snippet on the first frame should work.
Let's say you have a TextField on your stage with an instance name of counterTextField, and you want the count to start doubling up at frame 100.
import flash.events.Event;
var count:int = 0;
var limit:int = 100;
var increase:int = 1;
stage.addEventListener(Event.ENTER_FRAME, countFrames);
function countFrames(e:Event):void
{
if (count <= limit)
{
count += increase;
}
else
{
count *= 2;
}
counterTextField.text = String(count);
}

Changing the value of a Number Bug AS3

Hey everyone so I am having some trouble here. Been at it for an hour now and can't find a solution.
So I Have a movie clip named _Bunny added to the stage like so:
_Bunny = new mcBunny;
stage.addChild(_Bunny);
_Bunny.x = (stage.stageWidth / 2) - 225;
_Bunny.y = (stage.stageHeight / 2) - 330;
Now what this _Bunny does is move across the stage horizontally from right to left in a loop which i have set up like so in a Enter_Frame event listener:
private function bunnyView():void
{
_Bunny.x += nBunnySpeed;
if (_Bunny.x >=(stage.stageWidth / 2) + 215)
{
_Bunny.gotoAndStop("leftView");
nBunnySpeed--;
}
if (_Bunny.x <=(stage.stageWidth / 2) - 215)
{
_Bunny.gotoAndStop("rightView");
nBunnySpeed++;
}
}
It's speed is the nBunnySpeed which is equal to 5. Now I have another function that I am trying to change the value of the nBunnySpeed to say 20 whenever the nScore is equal to 1 like so:
private function updateDifficulty():void
{
if (nScore >= 1)
{
//Increase Speed
nBunnySpeed = 20;
}
but the bug that in which is produces is the Bunny shooting off to the right side of the screen which is the "+x" no matter what I do this always happens.
Can anyone see what I might be doing wrong? I don't understand why this is happening. Please help!
You need some kind of a flag that indicates that the difficulty has been already updated. Then, when you call updateDifficulty it first checks if there's a real need to update speed now, if there's none, it just returns. If yes, however, then you update your bunny's speed and set that flag so that the next time the function will not alter the bunny's speed.
var diffUpdated:Boolean=false;
private function updateDifficulty():void
{
if (diffUpdated) return; // here
if (nScore >= 1)
{
//Increase Speed
if (nBunnySpeed<0) nBunnySpeed=-20;
else nBunnySpeed = 20; // retain the direction of bunny's movement
}
diffUpdated=true;
}
Now, whenever you want your difficulty to be updated, you do diffUpdated=false; and voila, bunny's speed will be updated by this. For this, however, you will need more than two levels of speed, maybe one for 10 score, one for 50 and one for say 200.
What you do in this function
private function bunnyView():void
{
_Bunny.x += nBunnySpeed;
if (_Bunny.x >=(stage.stageWidth / 2) + 215)
{
_Bunny.gotoAndStop("leftView");
nBunnySpeed--;
}
if (_Bunny.x <=(stage.stageWidth / 2) - 215)
{
_Bunny.gotoAndStop("rightView");
nBunnySpeed++;
}
}
is check whether the _Bunny is offscreen or not. And if so, the nBunnySpeed will be nBunnySpeed - 1. But since BunnySpeed = 20, it will be 20 + 19 + 18 + 17, still going right. If you'd to turn it to BunnySpeed = -BunnySpeed, it will reverse immediately and go back.

5 Minute Countdown With Buttons

I have to make a 5 minute countdown clock as an assignment. I was given the basis of a as3 and told to add 'Start' - 'Reset' - 'Stop' buttons. So far I have a respectable countdown going, but no way to control it. I'm new to as2 and flash so I hope this isn't one of those "If you know that, you should know your answer" situations. Anything i try to find on the web as far as tuts don't really help me so if anyone could just tell me if I'm on the right path or eve if it's possible to ass buttons to this code to do whats stated above. Thanks in adv :)
var secs = "0" + 0;
var mins = 5;
timerDisplay.text = mins + ":" + secs;
var timerInterval = setInterval(countDown,1000);
//DISPLAYS DYNAMIC TEXT
function countDown()
{
secs--;
if (secs < 0)
{
secs = 59;
mins--;
}
if (secs < 10)
{
var secs2 = "0" + secs;
}
else
{
var secs2 = secs;
}
if (mins == 0 && secs == 0)
{
clearInterval(timerInterval);//STOPS TIME # ZERO
}
timerDisplay.text = mins + ":" + secs2;
}
As this is an assignment, I won't give you any code, just some ideas to get you on your way to writing your own.
You may want to add two (possibly three) buttons to the stage. One button could be the reset button and another could be a button that trades off between being start and stop (depending on whether the timer is currently running).
The only code necessary for a reset button, is something that sets your min variable and your secs variable back to their original values (possibly plus 1 second because of your interval code). If you need help getting started with buttons clicks throwing functions check out the mc.onPress method.
The only code necessary for a stop button, is something that stops your interval counter from continuing to count. I believe you already have something doing that when "clear" your timer at zero.
The only code necessary for a start button, is something that restarts your interval counter. You do something of the sort when you first start your timerInterval.
This won't work if someone decides to click on the start button after you have finished the countdown or if someone decides to click the start button multiple times.
In the first case, the countdown will continue into negative numbers, so you may want to write an if statement that doesn't allow that to happen (inside of the start button function).
And in the second case, the countdown will get faster and faster each time the button is pressed. Creating a boolean that keeps track of whether the program is stopped could possibly help with that problem.
To clarify a statement I made above about the interval code: Your code will forever decrement every 1000 milliseconds. The text box is only ever being refreshed when you decrement, so if you try to reset with exactly 5 minutes and zero seconds, you will see the numbers jump to 4:59 and then keep decrementing. If you reset to 5 minutes and 1 second, the numbers will appear to jump to 5:00 and then decrement from there.
I hope this helps!
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Test extends MovieClip {
private var _timer:Timer = null;
private var _repeatCount:int = 0;
private var _totalMinutes:int = 5;
public function Test() : void {
addEventListener(Event.ADDED_TO_STAGE, _Init);
}
private function _Init(e:Event) : void {
_repeatCount = _totalMinutes * 60;
_timer = new Timer(1000, _repeatCount);
_timer.addEventListener(TimerEvent.TIMER, _OnTimerFired);
_timer.start();
}
private function _OnTimerFired(e:TimerEvent) : void {
var minRem:int = (_repeatCount - _timer.currentCount) / 60 ;
var secRem:int = (_repeatCount - _timer.currentCount) % 60;
trace(minRem + ":" + secRem);
}
}
}

Actionscript 3: Healthbar and Button

So basically I am making a game in which a button is clicked to decrease the amount of health in a healthbar. I have a button on the stage named fortyfivedown_btn, and a healthbar, which is a 101 frame (includes zero) movieclip. The health bar has an instance name of lifebar. On the stage, the button coding is:
fortyfivedown_btn.addEventListener(MouseEvent.CLICK, fortyfivedownClick);
function fortyfivedownClick(event:MouseEvent):void{
lifebar.health = lifebar.health-45;
}
Inside the healthbar movieclip, I have a layer of coding that is:
var health:int = 100;
gotoAndStop(health + 1);
if(health < 0) health = 0;
else if(health > 100) health = 100;
gotoAndStop(health + 1);
So, there is my coding. The thing is, when the button is clicked, the healthbar does not go down. I traced the health variable in the button:
fortyfivedown_btn.addEventListener(MouseEvent.CLICK, fortyfivedownClick);
function fortyfivedownClick(event:MouseEvent):void{
lifebar.health = lifebar.health-45;
}
{
trace(lifebar.health);
}
I saw that the output is 0. For some reason the button believes the health is 0, when I declared it was 100 inside the healthbar movieclip? Any help is appreciated.
(Edit)
Alright, in answer to the trace question, if I don't do it like that, there is no output. I should say I'm a beginner at this all, and am learning as I go, so please bear with me. Here is my fla file:
https://skydrive.live.com/embed?cid=9AB08B59DCCDF9C6&resid=9AB08B59DCCDF9C6%21107&authkey=AGqFHhlHnvOXvuc
Okay so take the code that was in your lifebar movie clip out entirely, you can just delete that layer for now, then replace the code you have in the main scene with this and you should get the result you want:
fortyfivedown_btn.addEventListener(MouseEvent.CLICK, fortyfivedownClick);
var health:int = 100;
lifebar.gotoAndStop(101);
function fortyfivedownClick(event:MouseEvent):void{
health -= 45;
if(health < 0) health = 0;
else if(health > 100) health = 100;
lifebar.gotoAndStop(health + 1);
//Can write this also: lifebar.health += 45;
trace(health);
}
there's other ways to go about doing this but given your current setup this is going to be the least modification to get what you want. Another option is setting the width on a sprite.
I uploaded a modified fla and companion as file for how I would personally go about doing this instead of including things on the timeline so much:
http://www.mediafire.com/?d38hbm32p71x1n8