I am trying to make a dynamic text field that populates with a number when clicked. If 2 is shown in the text field, and the user clicks it, then 1 should show, and if 1 is shown, and the user clicks, 1 should show again. But with my code there seems to be a problem. Instead, when 2 is shown and it gets clicked, 1 shows, but if you click it again 2 doesn't show back up. How can I solve this?
import flash.events.MouseEvent;
var onoff:Number;
onoff = 2
options_mc.onoff_txt.text = String(onoff);
options_mc.onoff_txt.addEventListener(MouseEvent.CLICK, tick);
function tick(event:MouseEvent)
{
if(onoff = 1)
{
onoff = 2
options_mc.onoff_txt.text = String(onoff);
options_mc.onoff_txt.addEventListener(MouseEvent.CLICK, tick);
}
else(onoff = 2)
{
onoff = 1
options_mc.onoff_txt.text = String(onoff);
options_mc.onoff_txt.addEventListener(MouseEvent.CLICK, tick);
}
}
To set a value you should use =
The operator equal is ==
so when you test you should do :
if(onoff == 1)
All of this is well explained in this link
Related
I have these variables:
dynamic text 1 called ampere.
dynamic text 2 called tesla,
and the slider called ampereSlid.
I want to show the result from a mathematical operation that happened in dynamic text 1 on dynamic text 2 just by using the slider.
Here's my code:
stop();
import fl.events.SliderEvent;
import fl.controls.TextArea;
ampere.text = "0";
ampereSlider.value = 0;
tesla.text= "0"
ampereSlider.addEventListener (SliderEvent.CHANGE, changeAmpere);
function changeAmpere (event:SliderEvent):void{
ampere.text = event.target.value+"A";
}
ampereSlider.addEventListener (SliderEvent.CHANGE, changeTesla);
function changeTesla (event:SliderEvent):void{
var tsl:Number = 1.25664;
var tslnum = event.target as TextArea;
tesla.text = event.target.value;
}
Is it possible to make it so that when you click on a button the first time, a specific layer will become invisible... and then once you click on the button a second time, a different layer would become invisible, and so on? If so could I see an example? Thanks!
What I've tried :
/************************* RESET BUTTON **************************/
reset_btn.addEventListener(MouseEvent.CLICK,reset);
function reset(e:Event) : void
{
eraserClip.graphics.clear();
initEraser();
erasableBitmapData.fillRect(erasableBitmapData.rect, 0xFFFFFFFF);
penny.visible = true;
maskee4.visible = true;
card.visible = false;
greencard.visible = true;
}
The idea is, once I hit the reset button once, the layer named card, will disappear. Underneath that a layer will be there, which is titled greencard. Once I hit the reset button a second time I want the greencard to disappear. As you see above, I was just doing (property name).visible = false;. This works for the first card but not any after because they would not appear.
If I understand you correctly, you could try something like this below :
reset_btn.addEventListener(MouseEvent.CLICK, reset);
var clickCount : int = 0; //# start with zero since no clicks yet
card.visible = true;
greencard.visible = true;
function reset(e:Event) : void
{
clickCount += 1; //# adds +1 to current count of clicks
eraserClip.graphics.clear();
initEraser();
erasableBitmapData.fillRect(erasableBitmapData.rect, 0xFFFFFFFF);
penny.visible = maskee4.visible = true; //# if same value (true) you can chain them like this
if ( clickCount == 1) //if now 1 click
{
card.visible = false;
}
if ( clickCount == 2) //if now 2 clicks
{
greencard.visible = false;
}
}
Clicking one of 3 buttons shows price output in total.text
each button shows a different price.
code currently used for each button:
1 btn1.addEventListener(MouseEvent.CLICK, myButtonClick);
2
3 function myButtonClick(ev:MouseEvent):void
4 {
5 var a:Number = Number(99) * Number(1.2);
6 total.text = String(a);
7 }
8
9 btn2.addEventListener(MouseEvent.CLICK, myButtonClick);
10
11 function myButtonClick(ev:MouseEvent):void
12 {
13 var b:Number = Number(99) * Number(1.4);
14 total.text = String(b);
15 }
16
17 btn3.addEventListener(MouseEvent.CLICK, myButtonClick);
18
19 function myButtonClick(ev:MouseEvent):void
20 {
21 var c:Number = Number(99) * Number(1.6);
22 total.text = String(c);
23 }
24
it works fine, but i really want to Add the value of button 2 and/or 3 if toggled to the value of button 1 and display it in total.text
eventually, i am trying to get choice of 5 buttons to toggle 1 of. that toggle will set the initial Number(99) value with something..99 or what not. and then if i toggle 1 button in a second set of 5 buttons, it will set value in the second Number(1.2). and from there. the two toggled btn2 and btn3 would multiply total number in total.text.
i see where i can make a whole bunch of buttons with values calculated. but im sure there is a cleaner way to do that.
i am a total novice in flash and just kinda looking for a direction here. it would be awesome.
Edit: Did a little research. hope I'm heading in right direction
ok, added variable number like
var modifier1:Number = 99;
var modifier2:Number = 1.2;
then called on those from the function like
var a:Number = Number(modifier1) * Number(modifier2);
now researching on how to make the modifier1 actually link up to toggle state of a button and assign it a value like 99 for toggled and 1 for not.
I'd recommend creating a small class for all your buttons, it shall contain its value. You also need an object that will contain a list of these buttons, let's call it Application. When user clicks on Button1, it will set a field inside Application indicating the toggle - product of all values or just the button that was pressed. This then sets the actual text to whatever was passed (I've called it valueText).
http://puu.sh/d1hP7/e25228352b.png
Here's some quick code, I haven't tested if it actually works, but It should be pretty easy to follow:
class Button {
private var app:Application;
public var value:Number;
public function Button(app:Application, value:Number) {
this.app = app;
this.value = value;
this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
}
private function mouseDown(e:MouseEvent):void {
app.displayText(value);
}
}
class Application {
private var buttonList:Vector.<Button> = new Vector.<Button>();
public var isToggleOn:Boolean = false;
public function Application() {
buttonList.push(new Button(this, 0.98));
buttonList.push(new Button(this, 0.75));
buttonList.push(new Button(this, 0.88));
}
public function getProduct():Number {
var sum:Number = 0;
for(var button:Button in buttonList) {
sum += button.value;
}
return sum;
}
public function displayText(value:Number):void {
valueText.text = isToggleOn ? getProduct() : value;
}
}
I want to make a button where once the button is clicked it changes a dynamic text field from 100 to 0, and if clicked again from 0 to 100. When I exit the game, I want it to save what the user's last number was. If he exited with a "0" I want a 0 to show up the next time the user opens the game. I have made the following code:
import flash.events.MouseEvent;
import flash.media.SoundChannel;
import flash.ui.Mouse;
var onoff:Number;
onoff = 100
options_mc.onoff_txt.text = String(onoff);
options_mc.sound_btn.addEventListener(MouseEvent.CLICK, mute);
options_mc.test3.addEventListener(MouseEvent.CLICK, test3);
function mute(event:MouseEvent)
{
var so:SharedObject = SharedObject.getLocal("options");
if(so.data.onoff == 100)
{
so.data.onoff = 0
options_mc.onoff_txt.text = String(onoff);
so.flush();
}
else if(so.data.onoff == 0)
{
so.data.onoff = 100
options_mc.onoff_txt.text = String(onoff);
so.flush();
}
}
My problem with this code is that it's not changing the text field when the button is clicked! Could you please help on what I have done wrong?
I wouldn't initialize the sharedobject in a function, I would make it outside. However rather than explaining the entire reasoning, I would like you to this. Whole tutorial on saving and loading, as well as a clicking value which will be relevant to what you're doing.
I am trying to make an array of some of my sounds in my game. When a text field is clicked, the volume decreases or increases depending on the value of the number. I have tried doing this, but an error is occurring. What is wrong with my code?
import flash.events.MouseEvent;
import flash.media.SoundChannel;
var onoff:Number;
onoff = 2
var mysoundArray:Array = new Array(tchannel,Mchannel);
var volControl:SoundTransform = mysoundArray.soundTransform;
options_mc.onoff_txt.text = String(onoff);
options_mc.onoff_txt.addEventListener(MouseEvent.CLICK, tick);
function tick(event:MouseEvent)
{
if(onoff == 1)
{
onoff = 2
options_mc.onoff_txt.text = String(onoff);
volControl.volume = .5;
mysoundArray.soundTransform = volControl;
}
else if(onoff == 2)
{
onoff = 1
options_mc.onoff_txt.text = String(onoff);
volControl.volume = 1;
mysoundArray.soundTransform = volControl;
}
}
Your codes have multiple errors.
Error 1: where does tchannel and Mchannel come from?
var mysoundArray:Array = new Array(tchannel,Mchannel);
Error 2: Array mysoundArray does not have child object called soundTransform.
var volControl:SoundTransform = mysoundArray.soundTransform;
Therefore, the object volControl is null. Fix the above errors and your issue will be fixed.