Flash CS6, AS3 button issue - actionscript-3

I'm using the code below to have button "legalBtn" access the layer and instance name of "legalOverlay." Then after the legalese has been read, the enduser can close the "legalOverlay" via "closeBtn" and return to the last frame of the banner ad. The code below has no compiler errors, but the "legalBtn" is still not working. Any ideas?
import flash.events.MouseEvent;
function init(){
legalBtn.addEventListener(MouseEvent.CLICK, legalClick);
legalBtn.visible=true;
legalOverlay.clickthru.addEventListener(MouseEvent.CLICK);
legalOverlay.clickthru.buttonMode=true;
legalOverlay.closeBtn.addEventListener(MouseEvent.CLICK);
legalOverlay.closeBtn.buttonMode=true;
legalOverlay.visible=false;
}
function legalClick(e:MouseEvent){
if(legalOverlay.visible==true){
legalOverlay.visible=false;
} else {
legalOverlay.visible=true;
}
}
stop();

You have to call the init() function to add the click event listener on your legalBtn object and make it visible.

Related

FLASH AS3 Sound Overlap

Alright, I'm a total noob in flash as3 so this must be very easy to solve I guess. I'm making a soundboard with recorded voices in flash cs6, very simple: 1 frame, ten buttons, each button makes a different sound. The problem is the overlapping of these sounds, so what I need is that when I press one button the other sounds stop playing. anyone please?
See the play() method documentation in the Sound class, it returns a SoundChannel object, which has a stop() method.
So you could do it like that (schematically) :
var currentChannel:SoundChannel;
button1.addEventListener(MouseEvent.CLICK, onButtonClick);
button2.addEventListener(MouseEvent.CLICK, onButtonClick);
button3.addEventListener(MouseEvent.CLICK, onButtonClick);
function onButtonClick(event:MouseEvent):void
{
/* provided you have implemented selectSoundByButton func somewhere */
const sound:Sound = selectSoundByButton(event.currentTarget);
if (currentChannel) {
currentChannel.stop();
}
currentChannel = sound.play();
}
More detailed description:
Let's say you want to create yet another fart button application in flash. That's what you have to do:
Create a button symbol, add it to stage and give it an instance name in properties tab. Let's call it myButton
Add the sound to library with file->import
Export this sound to actionscript. Right click on a sound in library, check "Export for actionscript", "export in frame 1" on "actionscript tab". Fill "Class" input with a desired class name for a sound (e.g. MySound)
Then you have to trigger the sound playback on your button click. So you should put the following code to the first frame of your flash clip:
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
var currentChannel:SoundChannel;
const mySound:Sound = new MySound();
function onClick(e:MouseEvent):void {
if (currentChannel) {
currentChannel.stop();
}
currentChannel = mySound.play();
}
myButton.addEventListener(MouseEvent.CLICK, onClick);
Add this to your code on each button before playing a sound:
SoundMixer.stopAll();
If you're adding actions directly from the timeline in Adobe Flash, there's no need to import the class. If you're working from an IDE like FlashDevelop or FlashBuilder, add this code to the beginning (after Package {):
import flash.media.SoundMixer;
Happy coding!
Edit: More info on the SoundMixer class

Adobe Flash Pro CS6 movie looping after adding code to actionpanel

i have got a problem and cannot figure out. Everything is fine when i test the scene, i have added stop(); to my movie clip timeline and everything is fine until no code is added to the main stage. As soon as i start adding code to the stage, it starts looping? Because of this i'm stuck with a problem and cannot even develop further, because even mouse click events wont work of that looping... The code i am trying to add is just a simple move layer off stage after click on play button:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
introScreen.play_btn.addEventListener(MouseEvent.CLICK, clickAway);
function clickAway(event:MouseEvent):void
{
moveScreenOff(introScreen);
}
function moveScreenOff(screen:MovieClip):void
{
//Move the screen off...
var introTween = new Tween(screen,"x",Strong.easeInOut,screen.x,(screen.width)*-1,1,true);
//When the motion has finished...
introTween.addEventListener(TweenEvent.MOTION_FINISH, tweenFinish);
function tweenFinish(e:TweenEvent):void
{
trace("tweenFinish");
//Establish the game state...
gameState = STATE_INIT_GAME;
trace(gameState);
//Fire off the gameLoop function at the frame rate of the movie...
addEventListener(Event.ENTER_FRAME, gameLoop);
}
}
The reason your animations are looping continuously is because you have errors in your code.
Access of undefined property gameState.
Access of undefined property STATE_INIT_GAME.
Access of undefined property gameState.
Access of undefined property gameLoop.
You are trying to reference members that haven't been created yet or don't exist. Your main timeline starts at frame 1 with the code you referenced trying to access those variables and the gameLoop method. You need to setup the variables in the first frame to allow them to be referenced. i.e. under your import statements:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var gameState:String;
const STATE_INIT_GAME:String = "GAME_INIT";
then you need to add the gameLoop function:
function gameLoop(e:Event):void {
trace('game loop running');
}
If you don't want those variables and that function setup there then you need to go to the frame using gotoAndStop or gotoAndPlay where those functions and variables can be declared and/or initialized, and stored into memory. Then they will be accessible.

getURL in AS3, how to?

Hello I have not used flash in years, and need to use some old tracking code in my new AS3 banner. I noticed that in 3.0 you can't place actionscript on objects themselves, just on the timeline.
I need to convert this As2 script which i would normally have on a button labeled 'my_button'
on (release) {
getURL (_level0.clickTag, "_blank");
}
What is the equivalent code snippet to use on the timeline for my button labeled 'my_button'?
This is Google's clickTag suggestion:
import flash.events.MouseEvent;
import flash.net.URLRequest;
// ......
someButton_or_displayObject_to_receive_mouseClick.addEventListener(MouseEvent.CLICK,
function(event: MouseEvent) : void {
flash.net.navigateToURL(new URLRequest(root.loaderInfo.parameters.clickTAG), "_blank");
}
);
I saw a few diffrent examples, but none of them were identical to the method I use;
This is a full version, just copy and past and add in your URL AS3
import flash.events.MouseEvent;
import flash.net.URLRequest;
// add event listener to listen for "btnOpen" click
btnOpen.addEventListener(MouseEvent.CLICK,openFuction);
// add our openFuction
function openFuction(e:MouseEvent){
var openPage:URLRequest=new URLRequest("https://mysite.co.uk")
navigateToURL(openPage,"_blank");
}
There's another way, as well, which doesn't necessarily rely on the use of the ExternalInterface class. You could also try something like this (assuming your object on the stage has an instance name of "mybtn_mc":
mybtn_mc.addEventListener(MouseEvent.CLICK, openWebpage);
//
function openWebpage(e:MouseEvent):void {
var my_url:URLRequest = new URLRequest("http://stackoverflow.com/questions/15580069/geturl-in-as3-how-to/15639569");
flash.net.navigateToURL(my_url, "_self");
}
// if you want the object to act like buttons do in AS2
// with the handcursor effect when you mouse over them,
// you would include the following lines:
mybtn_mc.buttonMode = true;
mybtn_mc.useHandCursor = true;
This is written in timeline style, which is not something I approve of. But I sense you're not even close to ready to earning about Classes and how they work.
myButton.addEventListener(MouseEvent.CLICK, openWebpage);
function openWebpage(e:MouseEvent):void {
if (ExternalInterface.available) {
var callReturn:String = ExternalInterface.call('function(){window.open("http://www.yourURL.com", "_blank", "top=0,left=0,width=800,height=600");}');
}
}
Note that you should not be trying to reach outside the scope of the current MC, but how to do something else is beyond the scope of this answer har har. If interested, do a search on Dependency Injection.

Why is my button instance forgetting its textfield text and event listener?

I'm working on an assignment due at midnight tomorrow and I'm about to rip my hair out. I am fairly new to ActionScript and Flash Builder so this might be an easy problem to solve. I think I know what it is but I do not know for sure...
I'm developing a weather application. I designed the GUI in Flash CS5. The interface has 2 frames. The first frame is the menu which has a zipcode input and a button instance called "submit". On the second frame, it has another button instance called "change" which takes you back to the first frame, menu.
In Flash Builder 4, I wrote a class to extend that GUI called Application. When Main.as instantiates it, the constructor function runs. However, this was my first problem.
public class Application extends InputBase {
public function Application() {
super();
// Y U NO WORK???
this.gotoAndStop(1);
this.change.tfLabel.text = "Change";
}
}
When I ran debug it tossed a #1009 error saying it could not access the property or method of undefined object. It is defined on frame 2 in Flash CS5. I think this is the problem... Isn't ActionScript a frame based programming language? Like, you cannot access code from frame 2 on frame 1? But, I'm confused by this because I'm not coding on the timeline?
Anyway, I thought of a solution. It kinda works but its ugly.
public class Application extends InputBase {
public function Application() {
super();
// Y U NO WORK???
this.gotoAndStop(1); // when app is first ran, it will stop on the first frame which is the menu frame
setButton(this.submit, "Submit", 1);
setInput(this.tfZipCode);
}
private function submitZip(e:MouseEvent):void {
this.nextFrame();
setButton(this.change, "Change", 2);
}
private function menu(e:MouseEvent):void {
this.prevFrame();
setButton(this.submit, "Submit", 1); // if I comment this out, the submit button will return to the default text label and it forgets it event.
}
private function setButton(button:ButtonBase, string:String="", action:uint=0):void {
button.buttonMode = true;
button.mouseChildren = false;
button.tfLabel.selectable = false;
button.tfLabel.text = string;
switch (action) {
case 1:
button.addEventListener(MouseEvent.CLICK, submitZip);
break;
case 2:
button.addEventListener(MouseEvent.CLICK, menu);
break;
default:
trace("Action code was invalid or not specified.");
}
}
}
Its not my cup of tea to run the set button function every time one of the button instances are clicked. Is this caused by frames or something else I maybe looking over?
I believe you have two keyframes & both have an instance of the button component called button.
If you are working on the timeline try putting the button in a separate layer, with only one key frame.
Something like the following:
Ensures that you are referencing the same button every time & not spawning new ones on each frame.

How to check if the swf has been unfocused in AS3

I just thought it would be useful to know how to check if an swf has been focused or otherwise. This situation arises when the end user clicks outside of the stage (on the address bar of the browser, for example).
stage.addEventListener(Event.DEACTIVATE, notFocused);
stage.addEventListener(Event.ACTIVATE, focused);
function notFocused(e:Event) {
trace("Not focused");
}
function focused(e:Event) {
trace("Focused");
}
We can also avail NativeApplication for the same in AIR application.
import flash.desktop.NativeApplication;
import flash.events.Event;
NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, onActivate);
NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, onDeactivate);
function onActivate(e:Event){
trace("Activate");
}
function onDeactivate(e:Event){
trace("Deactivate");
}