I'm doing a simple 2 button menu. Each button is a movie clip with 3 labels for the states "none" "selected" and "hover". smartBtn needs to be set to "selected" on enter frame. When cinemaBtn gets clicked, smartBtn should go to its "none" state. But I'm not sure why smartBtn keeps on being selected.
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
var smartBtn = menu_mc.smart_mc;
var cinemaBtn = menu_mc.cinema_mc;
smartBtn.buttonMode = true;
cinemaBtn.buttonMode = true;
this.addEventListener(Event.ENTER_FRAME, EnterFrameHandler);
smartBtn.addEventListener(MouseEvent.CLICK, menuSmartClick);
cinemaBtn.addEventListener(MouseEvent.CLICK, menuCinemaClick);
function EnterFrameHandler(event:Event):void {
smartBtn.gotoAndStop("selected");
}
function menuSmartClick(e:MouseEvent) {
smartBtn.gotoAndStop("selected");
smartBtn.buttonMode = false;
cinemaBtn.gotoAndStop("none");
cinemaBtn.buttonMode = true;
}
function menuCinemaClick(e:MouseEvent) {
cinemaBtn.gotoAndStop("selected");
cinemaBtn.buttonMode = false;
smartBtn.gotoAndStop("none");
smartBtn.buttonMode = true;
}
ENTER_FRAME is fired at the begining of each frame, so smartBtn will be set to "selected" state every time even if you set it to "none" state.
Remove EnterFrameHandler call or add a test like this :
function EnterFrameHandler(event:Event):void {
if(cinemaBtn.currentFrameLabel != "selected")
smartBtn.gotoAndStop("selected");
}
Related
I've set up three buttons in the first frame which are supposed to switch frames. When the program first runs, there are no errors, and i can click any of the buttons and end up where I want. However, when going back to the first frame, the buttons no longer work. The stage listener still works though. I added the "Clicked" output to check if the function was called, which it wasn't. I know I disable the buttons in the code, but only after that button is clicked, and the task is done. I should mention I don't have the code on the timeline, but on a separate document. Here is my code:
package{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.SimpleButton;
public class DocumentClass extends MovieClip
{
public var matteOppgave:Boolean = false;
public var engelskOppgave:Boolean = false;
public var flaggOppgave:Boolean = false;
public function DocumentClass()
{
stop();
btnBok.addEventListener(MouseEvent.CLICK, matte);
btnFlagg.addEventListener(MouseEvent.CLICK, flagg);
btnPc.addEventListener(MouseEvent.CLICK, engelsk);
stage.addEventListener(Event.ENTER_FRAME, sjekk);
btnBok.enabled = true;
btnPc.enabled = true;
btnFlagg.enabled = true;
function matte(evt:MouseEvent)
{
gotoAndStop(2);
frame2();
trace("Clicked");
}
function engelsk(evt:MouseEvent)
{
gotoAndStop(3);
frame3();
trace("Clicked");
}
function flagg(evt:MouseEvent)
{
gotoAndStop(4);
frame4();
trace("Clicked");
}
function sjekk(evt:Event)
{
if(matteOppgave == true && engelskOppgave == true && flaggOppgave == true)
{
gotoAndStop(5);
}
if(matteOppgave == true)
{
btnBok.alpha = 0.5;
btnBok.enabled = false;
låsEin.alpha = 0;
}
if(engelskOppgave == true)
{
btnPc.alpha = 0.5;
btnPc.enabled = false;
låsTo.alpha = 0;
}
if(flaggOppgave == true)
{
btnFlagg.alpha = 0.5;
btnFlagg.enabled = false;
låsTre.alpha = 0;
}
}
}
I got it working from a solution posted on another forum. I made the five frames i had into movieclips, and changed back and forth using addChild() and removeChild().
I'm trying to create a beat em up game and right now i got my character attacking. I have 3 attack animation so far and works just about. If you keep mashing the attack button it will attack but the problem is it goes to the next attack animation as soon as the attack button is down and I don't want that. How can i make it go to the next attack animation once the current attack animation has finished instead of jumping to the next frame midway of it's current animation. So i want the character to finish its attack and if the player still keys in the attack key it will go to the next attack frame.
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Player extends MovieClip
{
//Attack variables
var Attacking:Boolean = false;
var Punches:int = 0;
var Punching:Boolean = false;
var Kicks:int = 0;
var Kicking:Boolean = false;
public function Player()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyPressed);
addEventListener(Event.ENTER_FRAME,Update);
}
function KeyPressed(event:KeyboardEvent):void
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
//If A key is down
if (event.keyCode == 65)
{
Attacking = true;
Punching = true;
Punches++;
}
}
function Update(event:Event)
{
//If player is not attacking
if (Attacking == false)
{
Punching = false;
Punches = 0;
Kicking = false;
Kicks = 0;
}
else if (Attacking == true)
{
if (Punching == true)
{
gotoAndStop('Jab' + Punches);
}
}
}
function KeyUp(event:KeyboardEvent)
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
}
}
}
Also within the last frame of every attack animation i have put down
import flash.display.MovieClip;
import flash.events.Event;
stop();
MovieClip(parent).Attacking = false;
MovieClip(parent).Punches = 0;
First of all you should remove the event listener adding and removing from both KeyPressed and KeyUp methods (they are not needed and they will just cause problemes) and register the key up event in the costructor just like the other two.
Secondly, to accomplish this you will need to check if the key is down instead of listening for the key press. To do this you will need a new field called, for instance, holdingAttackKey.
var holdingAttackKey:Boolean = false;
function KeyPressed(event:KeyboardEvent):void {
if (event.keyCode == 65)
{
holdingAttackKey = true;
Attacking = true;
Punching = true;
//...
function KeyUp(event:KeyboardEvent)
{
holdingAttackKey = false;
}
function Update(event:Event)
{
if(holdingAttackKey && Attacking==false) {
Attacking = true;
Punching = true;
Punches++;
}
//....
I have a problem with flash component ComboBox. When i wont to save a name in ComboBox its always without last letter!?
Here is the code:
var input_name:String;
text_field.addEventListener (TextEvent.TEXT_INPUT, text_input);
button.addEventListener (MouseEvent.CLICK, save);
function save (e:MouseEvent):void
{
text_field.visible = true;
text_field.adddEventListener(KeyboardEvent.KEY_DOWN, save_text);
}
function text_input(e:TextEvent):void
{
input_name = text_field.text;
}
function save_text(e:KeyboardEvent):void
{
var keyPressed:String = e.keyCode.toString();
if (keyPressed == "13")
{
combo.addItem({label:input_name, data:input_name});
}
}
So when i enter 'foo' in text field and press ENTER it saves in combobox just 'fo'...
Thx for answers :)
TextEvent.TEXT_INPUT is dispatched before the value has changed.
Event.CHANGE is dispatched after the value has changed.
This will work, including handling the enter key.
import flash.events.MouseEvent;
import flash.events.Event;
var input_name:String;
text_field.addEventListener(Event.CHANGE, text_input);
text_field.addEventListener("enter", save);
button.addEventListener(MouseEvent.CLICK, save);
function text_input(e:Event):void
{
input_name = text_field.text;
}
function save(e:*):void
{
text_field.visible = true;
combo.addItem({label:input_name, data:input_name});
}
So here is my question. I have a expandable banner. When expands a video is starting to play. My task was to insert an on/off button for the sound in the video, and i did that. But the problem is that i can't reach my button because when i try to move the mouse to the button the expandable area disappear because I'm moving to the button areas. Here is the code too.
import flash.events.Event;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.media.SoundTransform;
import flash.media.SoundMixer;
cierre.gotoAndStop(1);
SoundMixer.soundTransform = new SoundTransform(0);
video_player.autoPlay = true;
video_player.source = "video_500x374.f4v";
video_player.addEventListener(Event.COMPLETE, finVideo);
stop();
b2_btn.buttonMode = true;
b2_btn.addEventListener(MouseEvent.MOUSE_OUT, aBanner1);
b2_btn.addEventListener(MouseEvent.CLICK,onClick);
var clicktag=(stage.loaderInfo.parameters.clickTag)? stage.loaderInfo.parameters.clickTag:"http://www.vasava.es";
function onClick(e:MouseEvent){
navigateToURL(new URLRequest(clicktag),"_blank");
}
function aBanner1(e:Event):void{
video_player.stop();
this.gotoAndStop(1);
}
function finVideo(e:Event):void{
video_player.stop();
cierre.play();
}
function setMute(vol) {
var sTransform:SoundTransform = new SoundTransform (0,1);
sTransform.volume = vol;
SoundMixer.soundTransform = sTransform;
}
var Mute:Boolean = false;
mutebutton.addEventListener (MouseEvent.CLICK,toggleMuteBtn);
function toggleMuteBtn (event:Event) {
if(Mute) {
Mute = false;
setMute(0);
}else{
Mute = true;
setMute(1);
}
}
Either put the mute button in the expanded portion of the banner itself, or just have the expanding banner mute the audio automatically with no need for a second button push. Which to use depends on your specific application and client requirements.
Ive got a simple menu that upon hover of each item, plays a movie clip, then on mouse_out it plays the movie clip in reverse. What I'm trying to do is to have a third state (active) that is shown upon clicking. I'm thinking I need to do something along the lines of:
When clicked, gotoAndStop(5) //Five being the location of my active frame
Also remove the event listener that triggers the function to play the movie in reverse.
Then when another menu item is clicked, re-add the event listener to the previous menu item so it's not stuck 'active'
I can't quite figure out how to do this though. My code is as follows:
// IMPORTS
import fl.transitions.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.external.ExternalInterface;
// EVENT LISTENERS
//arrow
mcArrow.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcArrow.addEventListener(MouseEvent.MOUSE_OUT,mout);
//dots
mcDots.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcDots.addEventListener(MouseEvent.MOUSE_OUT,mout);
//music
mcMusic.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcMusic.addEventListener(MouseEvent.MOUSE_OUT,mout);
//home
mcHome.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcHome.addEventListener(MouseEvent.MOUSE_OUT,mout);
//padlock
mcPadlock.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcPadlock.addEventListener(MouseEvent.MOUSE_OUT,mout);
// FUNCTIONS
function mover(e:MouseEvent):void {
stopPlayReverse(e.currentTarget as MovieClip);
e.currentTarget.play();
//var fadeIn:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 0, 1, 0.5, true);
}
function mout(e:MouseEvent):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc !== null) {
mc.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
}
//var fadeOut:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 1, 0, 0.5, true);
}
function playReverse(e:Event):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc.currentFrame == 1) {
stopPlayReverse(mc);
} else {
mc.prevFrame();
}
}
function stopPlayReverse(mc:MovieClip):void {
if ((mc!==null) && mc.hasEventListener(Event.ENTER_FRAME)) {
mc.removeEventListener(Event.ENTER_FRAME, playReverse);
}
}
First I would create a MenuItem class that sets up the event listeners and methods for the menu items. In the MenuItem class, create an 'isActive' property of type Boolean that keeps track of the current state of the menu item. Here's a quick mock-up of that class:
package {
import flash.display.MovieClip;
import flash.events.*;
public class MenuItem extends MovieClip {
public var isOver:Boolean = false;
public var isActive:Boolean = false;
public static var CLICK:String = 'menu_item_click';
public function MenuItem() {
addEventListener(MouseEvent.MOUSE_OVER, mover);
addEventListener(MouseEvent.MOUSE_OUT, mout);
addEventListener(MouseEvent.CLICK, mclick);
addEventListener(Event.ENTER_FRAME, onFrame);
}
private function mover(e:MouseEvent):void {
isOver = true;
}
private function mout(e:MouseEvent):void {
isOver = false;
}
private function mclick(e:MouseEvent):void {
isActive = true;
goToAndStop(5); // go to active frame
}
private function onFrame(e:Event):void {
if (isActive) return; // don't do anything if this menu item is active
if (isOver) { // if the mouse is over the menu item
if (currentFrame >= 4) { // make sure we don't go to frame 5, the active frame
nextFrame();
}
} else {
prevFrame(); // or play in reverse. If at frame 1, prevFrame() won't do anything
}
}
}
}
The ENTER_FRAME listener will run continuously and perform actions based on the isActive and isOver states. This eliminates the need for removing listeners and creating additional methods for playReverse and stopPlayReverse.
Second, I would put all of the menu items in container class called MenuContainer. The easiest way to do this would be to create an empty movie clip in Flash, then drag all of your menu items into it. Export the container for Actionscript with the Class value of MenuContainer. Here is a mock-up of the MenuContainer class:
package {
import flash.display.MovieClip;
import flash.events.*;
public class MenuContainer extends MovieClip {
public function MenuContainer() {
addEventListener(MenuItem.CLICK, onMenuItemClick);
}
private function onMenuItemClick(e:MouseEvent):void {
var clickedMenuItem:MenuItem = MenuItem(e.target); // the clicked menu item
for (var i:int = 0; i < this.numChildren; i++) { // loop through the menu items
var menuItem:MenuItem = MenuItem(this.getChildAt(i)); // get the loop menu item
if (menuItem != clickedMenuItem) { // if the loop menu item is not the clicked menu item
menuItem.isActive = false; // then isActive is false
}
}
}
}
}
Another option, following your code, would be to have an auxiliary variable that indicates which menu item is currently active, so we can deactivate it once we click on another item... that means each of your menu items would execute this function on click:
private var activeMenuItem:MenuItem; //your auxiliary variable
private function onClick(e:MouseEvent):void {
if(activeMenuItem) {
//if there is an active menu item re-enable it
activeMenuItem.gotoAndStop(1);
activeMenuItem.mouseEnabled=true;
}
//set the active menu item to the clicked one
activeMenuItem=e.currentTarget;
//and set its state to active and mouse disabled
activeMenuItem.gotoAndStop(5);
activeMenuItem.mouseEnabled=false;
}