Custom Mouse Cursor dropping duplicate symbols after its been removed - actionscript-3

first of all, I'm a total noob to as3 and coding in general, I barely operate outside of code snippets.
I'm working on a project, and part of which is a scene where you get a custom mouse cursor upon entering the scene, and when you leave the scene, the custom mouse cursor is removed. The code I'm using to start the custom cursor is:
stage.addChild(crsTemple);
crsTemple.mouseEnabled = false;
crsTemple.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);
function fl_CustomMouseCursor(event:Event)
{
crsTemple.x = stage.mouseX;
crsTemple.y = stage.mouseY;
}
Mouse.hide();
with crsTemple being the instance name for the custom cursor. Then, when a new scene is entered (via rolling over an object), i have the following code in the new scene:
stage.addChild(crsTemple);
crsTemple.mouseEnabled = false;
crsTemple.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor_4);
function fl_CustomMouseCursor_4(event:Event)
{
crsTemple.x = stage.mouseX;
crsTemple.y = stage.mouseY;
}
Mouse.hide();
crsTemple.removeEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor_4);
stage.removeChild(crsTemple);
Mouse.show();
Unfortunately, whenever I go into the second scene, I get the regular mouse again, but it drops the crsTemple wherever the mouse was when the scene change happened, and it stays there for the rest of the time the file is running.
Any help is greatly appreciated, much thanks in advance for helping a noob like me!

No need to write the same code in new Scene. You can actually use all declarations form the first Scene. In the following code snippet MOUSE_MOVE handler (fl_CustomMouseCursor) from scene 1 will be called in scene 2 either. Custom cursor will also be accessible by its name crsTemple.
import flash.display.MovieClip;
import flash.events.MouseEvent;
var crsTemple:Sprite = new CrsTemple();
crsTemple.mouseEnabled = false;
addChild(crsTemple);
// for smooth cursor movement MOUSE_MOVE instead of ENTER_FRAME
stage.addEventListener(MouseEvent.MOUSE_MOVE, fl_CustomMouseCursor);
stage.addEventListener(MouseEvent.CLICK, nextStage); // for test purpose, just to switch the stage
function fl_CustomMouseCursor(event:Event):void
{
crsTemple.x = stage.mouseX;
crsTemple.y = stage.mouseY;
trace(crsTemple.x);
}
function nextStage(e:Event):void {
gotoAndStop(1,"Scene 2");
}
Mouse.hide();
stop();
here is a link to fla sample

Related

Animate CC advances to the next frame with gotoAndStop commented out?

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);
}
}

Adobe Flash/ActionScript 3.0 moving to next scene but nothing plays

I've searched endlessly for an answer but can't seem to find one.
I'm building a flash presentation for a client. It's got about 15 scenes, the .fla file is 200MB before publishing (26MB afterward). It's a pretty simple operation; I'have a pause/play & replay button, and once the scene is completed a next & replay button appears in the center of the stage.
My issue is when I get to about the 8th scene, halfway through. All my tweens and buttons stop working. Simple fade-in text doesn't come up and my pause/play & replay no longer function. I've tried shifting around scenes to see if it was anything in particular, but no matter what order and what scenes it always stops halfway through the 8th. I don't get any error notifications before, after or during the play. Tracing tells me that the button has been clicked and it's moved to the next scene but it simply will not play. Adding a play(); comment at the first frame of the next scene has not helped either.
Here are my functions that are on Scene 1 Frame 1
function pause_movie(event:MouseEvent):void {
stop();
playBtn.visible = true;
pauseBtn.visible = false;
}
function play_movie(event:MouseEvent):void {
play();
playBtn.visible = false;
pauseBtn.visible = true;
}
function replay_movie(event:MouseEvent):void {
gotoAndPlay(1);
}
function next_movie(event:MouseEvent):void {
this.nextScene();
trace("next_movie " + this.currentScene.name);
}
And then I just add event listeners when my buttons appear per scene
import flash.events.MouseEvent;
//Hide play button to start
playBtn.enabled = true;
pauseBtn.enabled = true;
playBtn.visible = false;
pauseBtn.addEventListener(MouseEvent.CLICK, pause_movie);
playBtn.addEventListener(MouseEvent.CLICK, play_movie);
replayBtn.addEventListener(MouseEvent.CLICK, replay_movie);
Any help is appreciated! Thank you!
I've created a new flash file with a blank canvas (just page number and next button). I'm loading the audio externally now as per #VC.One 's comment and the program stops working at the same scene regardless of which audio file I put in there.
Here is my updated code:
import flash.events.Event;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
var channel:SoundChannel = new SoundChannel;
var s:Sound = new Sound();
function newSound() {
s.removeEventListener(Event.COMPLETE, onSoundLoaded);
s = new Sound();
s.addEventListener(Event.COMPLETE, onSoundLoaded);
}
function onSoundLoaded(e:Event):void
{
channel = s.play();
}
function next_movie(event:MouseEvent):void
{
channel.stop();
try {
s.close();
}
catch(e:Error) {
trace("File already loaded");
}
this.nextScene();
trace("next_movie " + this.currentScene.name);
}
and each scene starts with:
pageNum.text = this.currentScene.name;
skipBtn.addEventListener(MouseEvent.CLICK, next_movie);
newSound();
s.load(new URLRequest('audio/ISB page 17 tk 1.mp3'));
Finally solved this issue. The scenes added together were more than the 16k frames Flash is apparently limited to. I had to convert each scene into a movieclip and put them in each of their own frame all in one scene with a bit of coding to play each one after the previous one finished. Everything works fine now. Thanks guys!

Why the background sound keep repeating?

I have one background sound to use in my project. But when I click the button to other scene, the sound repeats themselves and worse, it redundant with the sound in next scene. I was trying to use one background song for the whole project but this problem happened. Can teach me how?
I am using Adobe Flash CS6. Thank you.
the sound is repeating itself because timeline is repeating by default and you should add command stop() in actionscript. You did not provide any source code so I'll assume you don't have any, in your case here is a sample program in actionscript that loops the background music.
import flash.events.MouseEvent;
stop(); //<-- make sure to stop timeline
var bgMusic:Sound = new BGMusic();
var bgChannel:SoundChannel = new SoundChannel();
bgChannel = bgMusic.play();
bgChannel.addEventListener(Event.SOUND_COMPLETE, loop);
startButton.addEventListener(MouseEvent.CLICK, startGame);
function startGame(e:MouseEvent):void {
gotoAndStop(1, "Scene 2"); //<-- stop after clicking button
}
function loop(e:Event):void {
bgChannel = bgMusic.play();
bgChannel.addEventListener(Event.SOUND_COMPLETE, loop);
}

AS3 Creating a drawing app using bitmapdata

I'm trying to create a drawing application that renders vector lines into bitmap. I've read the documentation about bitmapdata and I have a basic understanding of how it should work. But I'm having some trouble. As of right now my goal is simple, allow the user to draw lines with their mouse, that's all I want. The problem is somewhere with the matrix i'm using, could someone help me out?
import flash.display.Sprite; //imports needed
import flash.events.Event;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.MouseEvent;
import flash.geom.Matrix;
var draw:Boolean = false; //Boolean to determine when the mouse is down since bitmapdata doesnt receieve mouse events.
var brush:Sprite =new Sprite(); // Creating the "brush", determining the stroke it will make.
brush.graphics.lineStyle(0x000000);
brush.graphics.lineTo(mouseX,mouseY);
var data:BitmapData = new BitmapData(600,400, false); // Creating bitmapdata to allow the work with pixels.
var canvas:Bitmap = new Bitmap(data);
addChild(canvas);
stage.addEventListener(MouseEvent.MOUSE_DOWN, drawStart); // Event listeners to determine when the mouse is up or down.
stage.addEventListener(MouseEvent.MOUSE_UP, drawStop);
stage.addEventListener(Event.ENTER_FRAME, render);
function drawStart(e:MouseEvent):void // When the mouse is down we are drawing
{
draw= true;
}
function drawStop(e:MouseEvent):void // When the mouse is up we are not drawing
{
draw= false;
}
function render(e:Event):void //Rendering the vector into bitmap
{
if(!draw) return;
var mat:Matrix=new Matrix(); // We need a matrix to get the correct mouse coordinates
mat.translate(mouseX,mouseY)
data.draw(brush,mat); // Then we draw the bitmap into vector.
}
I have listed comments to show what I understand is happening. If I have gotten something wrong i'd like it if someone could explain it better to me.
When tested, the program draws lines, but all it does is draw a line to the mouse position from some other seemingly random position. So I figure the problem has something to do with the matrix.
I appreciate any help I can get, i've been looking at this for a while and it's just not clicking. Thanks.
The main problem with your code is that you draw the line into your bush.graphics only once (when your app starts), before any user input and then draw that same line into your bitmap data every frame as long as the mouse is down.
One way to do things correctly would be to redraw that line every frame while the user keeps the mouse key down. The drawing should happen in your brush.graphics (which now serves more like a canvas) and finally, once the user releases the mouse the line he has drawn should be rendered into the bitmap data so you can reuse your brush.graphics for drawing new lines.
var draw:Boolean = false; //Boolean to determine when the mouse is down since bitmapdata doesnt receieve mouse events.
var brush:Sprite;
var canvas:Bitmap;
var data:BitmapData;
var start:Point = new Point();
brush = new Sprite(); // This will serve as a canvas
data = new BitmapData(600,400, false); // Creating bitmapdata to allow the work with pixels.
canvas = new Bitmap(data);
addChild(canvas);
addChild(brush); // Add to display list so we can see what we are drawing visually
stage.addEventListener(MouseEvent.MOUSE_DOWN, drawStart); // Event listeners to determine when the mouse is up or down.
stage.addEventListener(MouseEvent.MOUSE_UP, drawStop);
stage.addEventListener(Event.ENTER_FRAME, render);
private function drawStart(e:MouseEvent):void // When the mouse is down we are drawing
{
draw = true;
start.setTo(e.localX, e.localY); // Save mouse position at interaction start
}
private function drawStop(e:MouseEvent):void // When the mouse is up we are not drawing
{
draw = false;
data.draw(brush, null); // User released the mouse and we can draw the result into bitmap
}
private function render(e:Event):void //Rendering the vector into bitmap
{
if(!draw) return;
// Redraw the line each frame as long as the mouse is down
brush.graphics.clear();
brush.graphics.lineStyle(0x000000);
brush.graphics.moveTo(start.x, start.y);
brush.graphics.lineTo(mouseX, mouseY);
}

Keyboard AS3 detection

I know there's a lot of questions on this but I'm really having troubles getting this to work.
I only have in the first frame this code:
var game = new Game(this);
In the game class I have a lot of stuff
package {
import flash.display.*;
import flash.ui.*;
import flash.events.*;
public class Game extends MovieClip {
public function Game(esc) {
var camp = new Camp(); //camp és l'escenari, el conjunt de celles
var player = new Player();
esc.addEventListener(KeyboardEvent.KEY_UP, controlTeclat);
camp.mostraInterficie(esc);
player.situaPlayer(esc);
}
public function controlTeclat(ev){
switch(ev.keyCode){
/*case 37: player.moveLeft();break;
case 38: player.moveUp();break;
case 39: player.moveRight();break;
case 40: player.moveDown();break;
case 32: player.dropBomb();break;*/
}
trace ("hi");
}
}
}
The problem is that the controlaTeclat() function's never called, the trace is no printed. No error displayed, dough.
The mc will need to be on the displayList to receive keyboard events.
var game = new Game(this);
addChild( game );
Without more code it's hard to say exactly what's going wrong here, however if the esc object doesn't have focus (hasn't been clicked by the mouse) then keyboard events won't propagate through it and so the handler won't fire.
You could just add the keyboard listener to the stage itself. You can also set the focus with 'stage.focus' so it will receive events without having to click on the stage first.
stage.addEventListener( KeyboardEvent.KEY_UP, keyupHandler );
//if you want to, you can set focus like this:
stage.focus = stage; //or some other object
private function keyupHandler(e:KeyboardEvent):void
{
trace("keyupHandler()");
}
if (esc.stage) esc.stage.addEventListener(KeyboardEvent.KEY_UP, controlTeclat);
else trace("Stage is inaccessible!");
The best practice is allocating your keyboard listeners to stage, so that they will always react to keyboard events. "esc" is your Document class seemingly, but it's not the stage, so we use "stage" property of "esc" to get access there.