Having trouble with my Flash Comic - actionscript-3

I'm making a webcomic in Adobe Animate right now, and the way a viewer can turn the "page" is by clicking on the screen.
First, I put a code on one frame shown as follows:
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
stop();
stage.addEventListener(MouseEvent.CLICK,forward2);
stage.addEventListener (KeyboardEvent.KEY_DOWN,forward);
function forward2 (event:MouseEvent) :void {
gotoAndStop(currentFrame+1);
}
function forward (e:KeyboardEvent): void {
if (e.keyCode == Keyboard.RIGHT)
gotoAndStop(currentFrame+1);
}
Then, if I wanted a scene to play out without the viewer having to click on the screen, I wrote another code:
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
stop();
stage.addEventListener(MouseEvent.CLICK,forward4);
stage.addEventListener (KeyboardEvent.KEY_DOWN,forward3);
function forward4 (event:MouseEvent) :void {
gotoAndPlay(currentFrame+1);
}
function forward3 (e:KeyboardEvent): void {
if (e.keyCode == Keyboard.RIGHT)
gotoAndPlay(currentFrame+1);
}
So because the function was already defined on another frame, I posted this code on the frame after the animation:
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
stop();
stage.addEventListener(MouseEvent.CLICK,forward2);
stage.addEventListener (KeyboardEvent.KEY_DOWN,forward);
I tried to make sure that the script was constant throughout the frames I wanted it to be used for by inserting a blank frame for each frame I wanted it to be active on. However, when I tested it, as soon as I clicked on the frame I put the code on (The one without the function defined), it just sent me back to the beginning.
What am I doing wrong?

When you add a listener to any object it will continue to exist until it is explicitly removed.
Before going to the next frame you should run:
stage.removeEventListener(MouseEvent.CLICK, forward2);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, forward);
This will remove the event listeners and prevent forward2 and forward from running when you click or press a key on some other frame.

Related

Move projectile to mouse-click position AS3

I have a cannon and cannonball. How do I make a cannonball to move in a line from cannon to the Mouse Click position and stop/disappear/activate Explode animation?
I've tried different solutions and none of them would seem to work for me so I cleared it a bit.
And yes, I know it's ugly.
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Mouse;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.utils.Timer;
import flash.display.Sprite;
addEventListener(Event.ENTER_FRAME, enterFr);
function enterFr(e:Event)
{
aims.x = mouseX;
aims.y = mouseY;
}
Mouse.hide();
zamok.addEventListener(MouseEvent.CLICK, fire);
function fire(m:MouseEvent)
{
var s:Sound = new cannonFire();
s.play();
var explo:boom = new boom();
explo.x = mouseX;
explo.y = mouseY;
addChild(explo);
}
Well you should think of the process that you want to implement.
First of all it is not instant, it takes some time for cannonball to move to the point where mouse clicked.
Let's start with some function that will create a cannonball:
private function fireCannonBall(target: Point):void
{
const cannonBall:CannonBall = new CannonBall(); // you need to implement this class, or just use some MovieClip from library;
cannonBall.x = initialPosition.x; // initial position is a point where your cannon is located.
cannonBall.y = initialPosition.y;
addChild(cannonBall);
// I suggest using TweenNano, but it has some limitations, read the License Agreement carefully
TweenNano.to(cannonBall, 15 /* animation duration */, {x: target.x, y: target.y, onComplete: makeExplosion, onCompleteParams: [cannonBall]});
}
private function makeExplosion(cannonBall: CannonBall):void
{
/* I leave this part to you, here you might want to launch some explosion animation */
}
Now we need to handle the click:
private function onMouseClick(e: MouseEvent):void
{
const target: Point = new Point(stage.mouseX, stage.mouseY);
//and launch the cannonBall:
fireCannonBall(target);
}
That's it, roughly.
To know more about TweenNano please follow the link:
https://greensock.com/tweennano-as

Actionscript3 clearInterval is not working on TouchEvent.TOUCH_BEGIN

I have called a setInterval on TouchEvent.TOUCH_END and I want to clear it when ever screen is touched.
Here is my code:
import fl.motion.MotionEvent;
import flash.display.MovieClip;
import flash.utils.*;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
stage.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
function onTouchBegin(evt:TouchEvent)
{
clearInterval(MovieClip(root).myInterval);
}
function onTouchEnd(evt:TouchEvent)
{
MovieClip(root).myInterval = setInterval(showTimer,1000);
}
function showTimer()
{
trace("interval working");
}
Your provided code doesn't really explain everything so there will be a fair bit of speculation in this answer:
Possiblity No. 1:
Your way of casting root to MovieClip is not working. Try changing to following:
function onTouchBegin(evt:TouchEvent)
{
var intervalRef:int = (root as MovieClip).myInterval;
clearInterval(intervalRef);
}
Possiblity No. 2:
It seems that your code is written in a frame. And although if you define something in a previous frame it should work, but if you have the code on a different (lower) layer on the same keyframe it could define the variable later and your MovieClip(root).myInterval variable is undefined or null is the value is not defined. So check if your variable even exists:
function onTouchBegin(evt:TouchEvent)
{
trace(MovieClip(root) == null); // see if the root is defined
trace(MovieClip(root).myInterval); // see if the myInterval is defined
}
Possiblity No. 3:
You're cycling through frames. When I had 2 frames: one blank and one with your code, the code didn't work properly. Tested in Flash CC.
Possibility No. 4:
You have a run-time error somewhere else and your whole frame's code is not being executed. Do you use the debug version of Flash Player?
Possible solution for less headache:
Use a Timer. It's easy to control, easy to manage and dispose of. I've edited your code to work with Timer and tested. Feel free to use it for your project.
import fl.motion.MotionEvent;
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
var timer:Timer = (timer == null) ? new Timer(1000) : timer;
timer.addEventListener(TimerEvent.TIMER, onTick);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onTouchBegin); // you can change back to TouchEvent, but since you're using TOUCH_POINT it's pointless so I'd just use MouseEvent
stage.addEventListener(MouseEvent.MOUSE_UP, onTouchEnd);
function onTouchBegin(evt:Event)
{
timer.stop();
}
function onTouchEnd(evt:Event)
{
timer.start();
}
function showTimer()
{
trace("interval working");
}
function onTick(e:TimerEvent):void
{
showTimer();
}
I do want to note that your code works if I just copy-paste it in a single frame Flash file. But still, it's encouraged by Adobe to use Timer instead.

Error 1026 Action Script 3

I'm trying to make a simple button with gotoAndStop command in AS3, but i keep getting error 1026. This is the code:
import flash.events.MouseEvent;
function onButtonClick(event:MouseEvent):void
{
gotoAndStop(10);
}
btn1.addEventListener(MouseEvent.CLICK, onButtonClick);
there are no other code except stop(); at frame 1
any help?

remove and addchild button in as3.0

I want to click on the button "choose1" then button will disappear and the next is to add a button.
I would like to make two requests in a click event of the button "choose1".
EDIT: this is my code
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Stage;
public class Bo extends MovieClip
{
var choose1:Choose;
public function Bo()
{
// constructor code
choose1=new Choose();
addChild(choose1);
choose1.addEventListener(MouseEvent.CLICK,clickChoose);
}
function clickChoose(e:MouseEvent):void
{
for (var i:Number=stage.numChildren -1; i>=0; i--)
{
stage.removeChildAt(i);
//stage.getChildAt(i).visible = false;
}
var ex:Next=new Next();// add new button
addChild(ex);
//this.stage.removeEventListener(MouseEvent.CLICK,clickChoose);
}
}
anyone can help me? thanks.
Removing a button after click is bad UX, consider adding whichever button you want but keeping the old one.

ActionScript 3 Flash error 1009 and 2025 combo box

Im trying to insert a music using combo box from the component asset. yesterday works fine, but today suddenly it stopped working and it gave me this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
everytime I clicked on the combo box these error appears, strangely it works fine yesterday.
this is the code for the first frame:
import flash.events.Event;
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.Event;
import flash.media.SoundTransform;
import flash.events.MouseEvent;
//Declare MUSIC Variables
var musicSelection:Sound;
var musicChannel:SoundChannel;
var musicTrack:String = "music/dearly_beloved.mp3";
var musicVolume:Number=0.2;
/*var isMuted = false;*/
loadMusic();
function loadMusic():void
{
//first stop old sound playing
SoundMixer.stopAll();
musicSelection = new Sound();
musicChannel = new SoundChannel();
//create and load the required soun
musicSelection.load(new URLRequest(musicTrack));
//when loaded - play it
musicSelection.addEventListener(Event.COMPLETE, musicLoaded);
}
function musicLoaded(evt:Event):void
{
//finished with this listener
musicSelection.removeEventListener(Event.COMPLETE, musicLoaded);
//play music
playMusic();
}
function playMusic():void
{
//play the random or selected music
musicChannel = musicSelection.play();
//setting the volume control property to the music channel
musicChannel.soundTransform = new SoundTransform(musicVolume, 0);
//but add this one to make repeats
musicChannel.addEventListener(Event.SOUND_COMPLETE, playAgain);
}
function playAgain(evt:Event):void {
// remove this listener and repeat
musicChannel.removeEventListener(Event.SOUND_COMPLETE, playAgain);
playMusic();
}
and this is the code for the second frame:
import flash.events.Event;
menuBtn.addEventListener(MouseEvent.CLICK, goToMenu)
function goToMenu(evt:Event):void
{
gotoAndPlay(2);
}
// BUTTON EVENT LISTENERS
musicCombo.addEventListener(Event.CHANGE, updateMusic);
volumeSL.addEventListener(Event.CHANGE, setSlider);
//process COMBO BOX changes
function updateMusic(evt:Event):void
{
if (musicCombo.selectedItem.data == "none")
{
//no music is required so stop sound playing
SoundMixer.stopAll();
}
else
{
//otherwise load in the selected music
musicTrack = "music/" + musicCombo.selectedItem.data;
loadMusic();
}
}
function setSlider(evt:Event):void
{
//identify the button clicked
var mySlider:Object = (evt.target);
//adjusting to volume of the music channel
musicVolume = mySlider.value;
musicChannel.soundTransform = new SoundTransform(musicVolume, 0);
}
the error occurs whenever I clicked on the combo box and when I tried to insert another combo box without putting any data, the same error occurs. hope you guys can help me ASAP cause this is due on friday this week xD
thanks
Alright, found the answer. It seems I need to put these 3 lines of code:
import fl.events.ComponentEvent;
import fl.controls.ComboBox;
import fl.data.DataProvider;