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.
Related
I'm writing this code that tests your reaction time and then advances to the next frame. It shows a box and then time the difference between when the box appeared and when the use presses [A]. Heer is my code
import flash.utils.Timer;
import flash.events.Event;
import flash.utils.getTimer;
stop();
var canPress = false;
var startClock:Timer = new Timer(4000+Math.random()*6000, 1);
grbox.y = -500;
startClock.start();
var startTime:int = 0;
function displayBox(evt:Event):void{
canPress = true;
grbox.y = 143;
var startTime:int = getTimer();
}
function Tpressed(e:KeyboardEvent):void
{
if(e.keyCode==Keyboard.A){
if(canPress==true){
var endTime:int = getTimer();
score1 = endTime-startTime;
if(score2<0){
//gotoAndStop(3);
}
else{
//gotoAndStop(4);
}
}
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, Tpressed);
startClock.addEventListener(TimerEvent.TIMER, displayBox);
For some reason if I just spam the [A] button it will advance to the next frame. Why is this happening?!?! My 'gotoAndStop(4);' command is commented out so it should do anything, yet it is.
EDIT: Here is my .fla file: https://drive.google.com/open?id=0BxtLreFIVnSWR2VPSGdSaHZGaVk
RAW CODE: https://docs.google.com/document/d/1GRZIaKAdRNu3z3aPjjXNcgqMl2BhR-ZBT6gU7OeSbWQ/edit?usp=sharing
On one of your frames you added an event listener for key presses to the stage. That's probably where your problem is at. So when you press any key, it calls the pressed function as well as the Tpressed function. And since the key that is being checked for in each function is "A", both functions execute their if blocks. And both if blocks call a gotoAndStop method.
Without knowing exactly what you are trying to accomplish in the big picture, this problem could be fixed by removing the event listener for the pressed function when you leave that frame.
Could look like:
function pressed(e:KeyboardEvent):void
{
if(e.keyCode==Keyboard.A){
gotoAndStop(Math.round(Math.random()+2));
// remove the event listener since we are leaving this frame and you apparently only want this function to work on this frame
stage.removeEventListener(KeyboardEvent.KEY_DOWN, pressed);
}
}
I have a preload movie that loads an interface. In the interface I have many buttons. When I click on a button I want to load a text file into a movie called "TextMovie.swf". I want to be able to click on a button, take the name of the button and load a text file that is the same name as the button instance, and have the window appear close to the mouse click. There will be hundreds of buttons and hundreds of text files. I'm sure this is easy for some. Here is the loader for a button:
button_1.addEventListener(MouseEvent.CLICK, fl_ClickToPosition_2);
function fl_ClickToPosition_2(event:MouseEvent):void
{
var my2ndLoader:Loader = new Loader();
var url2:URLRequest = new URLRequest("TextMovie.swf");
my2ndLoader.load(url2);
addChild(my2ndLoader);
my2ndLoader.x = 200;
my2ndLoader.y = 10;
}
Then in the TextMovie.swf I have:
import flash.net.URLLoader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.text.StyleSheet;
var css:StyleSheet = new StyleSheet();
css.setStyle("a:hover", {textDecoration:"underline"});
var textLoader: URLLoader = new URLLoader();
textLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
textLoader.addEventListener(Event.COMPLETE, textLoaded);
loadText();
function textLoaded(evt: Event): void {
title_txt.htmlText = textLoader.data.txtTitle;
main_txt.htmlText = textLoader.data.txtBody;
URL_txt.htmlText = textLoader.data.txtURL;
URL_txt.styleSheet = css
}
function loadText(): void {
// var fileName: String = "assets/test_01.txt";
var fileName: String = "assets/" + + ".txt";
textLoader.load(new URLRequest(fileName));
}
I'm not sure how to get the name of the button I pushed and apply it to the string that will load the correct text file. I also can't get the window to appear close to the mouse click. Right now I just have it set with an x and y for testing.
I know it's a lot but thanks for any help.
I suggest you take a look at the docs for MouseEvent so you better understand what data is passed as parameters when the event is triggered.
In the event handler, a reference is passed to the object which was clicked: the event target– and from that you can get its name, location, etc. This is good because it means you only need one event handler for all your buttons.
Once you have the name, you can build the string you need for your url. (I wouldn't do it that way if it is a project which is going to have any sort of lifespan but it's an ok approach).
You can get the x and y location of the target, its width and height, and use that data to position your window. The localX and localY properties refer to the click location relative to the rect of the target (button) – with top/left of the target== 0,0.
This is the trace output from the function below:
red red.swf
600 400
25 8.5
button_1.addEventListener(MouseEvent.CLICK, onHandleButtonClick);
function onHandleButtonClick(event:MouseEvent):void
{
var btnName:String = event.target.name;
var swfName:String = btnName + ".swf";
trace(btnName, swfName);
trace(event.target.x, event.target.y);
trace(event.localX, event.localY);
}
Im new to ActionScript 3 and am trying to get a number to deplete when i start a timer
So far ive got it so you click the button 'btngo' and a timer begins whilst the ball object moves.
I also want a dynamic text that starts at 100 to deplete down to 0 in 10 seconds when this button is clicked, but unfortunately i have no clue how to do it
Any help will be greatly appreciated, thank you :)
import flash.utils.Timer;
import flash.events.TimerEvent;
stop();
var timer1:Timer = new Timer (10,10000);
var timer2:Timer = new Timer (10,10000);
timer1.addEventListener(TimerEvent.TIMER, forward);
timer2.addEventListener(TimerEvent.TIMER, back);
btngo.addEventListener(MouseEvent.CLICK, green);
btnstop.addEventListener(MouseEvent.CLICK, red);
function green(e:MouseEvent):void {
timer2.stop();
timer1.start();
trace("timer started");
}
function red(e:MouseEvent):void {
timer1.stop();
timer2.start();
trace("timer started");
}
function forward(e:TimerEvent):void {
ball.x += 2;
}
function back(e:TimerEvent):void {
ball.x -= 2;
}
Well, here are two functions which will get you what you are looking for. This first one is the creation of the text:
import flash.text.TextField;
import flash.text.TextFieldType;
var dynamic_text:TextField;
function CreateText()
{
// You can also replace this part with code that creates a textfield that you have in your library,
// in that case just make sure the textfield is set to dynamic in the textfield properties
// Create a new textfield.
dynamic_text = new TextField();
// Make sure its type is set to Dynamic.
dynamic_text.type = TextFieldType.DYNAMIC;
// Position it somewhere on the screen.
dynamic_text.x = 10;
dynamic_text.y = 10;
// Set a default text for now.
dynamic_text.text = "Time: " + time_left;
// Make sure the players cannot select the text.
dynamic_text.selectable = false;
// Add it to your stage (or other parent, take your pick)
stage.addChild(dynamic_text);
}
This second one is called every TimerEvent:
function OnTimerChange()
{
// Update the text based on the new time.
dynamic_text.text = "Time: " + time_left;
}
You will have to call this OnTimerChange function inside your own "forward" and "backward" functions, and in those two functions you will have to calculate how much time has passed.
For an example how to keep track of time that has passed since you pressed a button:
actionscript 3 how to keep track of time elapsed?
Hope this helped.
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
I am fairly new to AS3, so here is my problem.
I have two text input boxes p1 and p2. I want to do a conditional test to see if p1 has the focus. If it does not, then p2 must have the focus. Here is some code I am trying to get to work.
if ((Selection.getFocus()) == (p1totalScore.text)){
p1Score();
} p2Score();
Thanks for your help.
David
Without a FocusManager, you could test what the stage is returning for focus:
(If you have textInput1 and textInput2 on the art-board)
import flash.events.MouseEvent;
import fl.controls.TextInput;
var textInput1:TextInput;
var textInput2:TextInput;
stage.addEventListener(MouseEvent.CLICK, mouseClickHandler);
function mouseClickHandler(event:MouseEvent):void
{
if(stage.focus == textInput1.textField)
trace("text field 1 has focus.");
else if(stage.focus == textInput2.textField)
trace("Text field 2 has focus.");
}
I think a better approach than you're attempting is to add event handlers for focus change:
import fl.controls.TextInput;
import fl.managers.FocusManager;
import flash.events.FocusEvent;
var textInput1:TextInput;
var textInput2:TextInput;
var focusManager:FocusManager = new FocusManager(this);
textInput1.addEventListener(FocusEvent.FOCUS_IN, textInput1FocusHandler);
textInput2.addEventListener(FocusEvent.FOCUS_IN, textInput2FocusHandler);
function textInput1FocusHandler(event:FocusEvent):void
{
trace("textInput1 has focus.");
}
function textInput2FocusHandler(event:FocusEvent):void
{
trace("textInput2 has focus.");
}
Adobe will get you 99% of the way:
'http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/managers/FocusManager.html'
I just changed the function to return the name of the control that has focus, instead of the instance name that their example returns.
private function focusChange(e:FocusEvent):void {
var ti_now:InteractiveObject;
ti_now = fm.getFocus();
trace("Focus now: ", ti_now.name);
}