A conflict exists with definition event in namespace internal - actionscript-3

I am new in as3. I get this error:
A conflict exists with definition event in namespace internal.
I'm using same coding but different scene. I changed second scene.
var mysound:bila =new bila()
This is the code of my 1st scene, and success only in 1st scene:
var isItPlaying:Boolean = true;
var lastposition:Number = 0;
var mysound:izhar = new izhar();
var soundchannel:SoundChannel = new SoundChannel();
soundchannel = mysound.play(0,0);// playing sound in the channel
soundchannel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete1);
function onPlaybackComplete1(event:Event):void
{
lastposition = 0;
soundchannel.stop();
btn.btn_pause.visible = false;
trace("finished");
isItPlaying=false;
}
/************end of part 1********/
btn.addEventListener(MouseEvent.CLICK, playsound);*
function playsound(event:MouseEvent):void*
{
if (! isItPlaying)
{
soundchannel = mysound.play(lastposition,0);
btn.btn_pause.visible = true;
isItPlaying = true;
}
else
{
lastposition = soundchannel.position;
soundchannel.stop();
btn.btn_pause.visible = false;
/*trace(lastposition.toFixed(0), mysound.length.toFixed(0));*/
isItPlaying = false;
}
soundchannel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);*
function onPlaybackComplete(event:Event):void
{
lastposition = 0;
soundchannel.stop();
btn.btn_pause.visible = false;
trace("finished");
isItPlaying=false;
}
}

The conflict is because you are declaring two variables with the same name in the same scope.
You declare the event variable as a parameter in your playsound function. Then you have a nested function onPlaybackComplete that declares a parameter with the same name.
To fix this, either rename one of the parameters to something else, or move the onPlaybackComplete function outside the playsound function so it has its own scope.
I'd prefer the second option, unless you need have that function nested for some specific reason.

Related

actionscript 3 Timer class and callback problems

I have a timer class in actionscript that looks like this:
public class AGTimer {
public var myTimer:Timer ;
public var done:Boolean = false;
public var timer_disable:Boolean = false;
public var started:Boolean = false;
public function AGTimer(num:Number = 0) {
this.timer_disable = false;
if (num != 0 ) timerStart(num);
// constructor code
}
public function timerStart(num:Number):void {
myTimer = new Timer(num * 1000, 1);
myTimer.addEventListener(TimerEvent.TIMER, runOnce);
myTimer.start();
started = true;
done = false;
}
public function runOnce(e:TimerEvent):void {
done = true;
}
public function timerDone():Boolean {
if (done && ! timer_disable) return true;
else if (!started ) return true;
else return false;
}
}
I have several of these in an array. I reference them with their index number. sometimes I start a timer and let it go, then before I have a chance to check it, I delete it by calling 'new AGTimer()' and creating a new one. Could it happen that the callback from the actionscript Timer object is still called somehow after the timer itself is deleted? What would I do about it if it did happen? I am trying to find a bug and am considering this sort of problem. What kind of error would I see?
Yes, a timer will continue to run callbacks even if it is not referenced somewhere. You have to remove event listener for the timer for it in order to remove it.
Basically something like this:
private function destroyTimer():void {
if(myTimer) {
myTimer.removeEventListener(runOnce);
myTimer.stop();
myTimer = null;
}
}

Accessing event.target or event.currentTarget beyond the listener Function

Originally I had two functions, the first function being dataLoaded which had an eventListener in it that spawned a new function called itemClicked. But if I wanted to add a delayedFunction and have itemClicked as an intermittent stage between dataLoaded and delayedFunction, how can I access the event.target or event.currentTarget of the original listener when I come to reference it in my delayedFunction?
private function dataLoaded(event:Event):void {
//items properties call - add other calls to master properties later on
items = data.item;
// parsing of each ingredient
for (var i = 0; i < items.length(); i++) {
// instantiation of mcItem (the stage for each item)
_item = new Item();
// sets //over// layer to invisible / transparent
_item.item_btn_over.alpha = 0;
// creates the var itemTextField
_itemTextField = new TextField();
_itemTextField.text = items[i].toString();
// adds textfield to displaylist
_item.addChild(_itemTextField);
//adds items to container displaylist
_container.addChild(_item);
}
_item.parent.addEventListener( MouseEvent.CLICK, itemClicked );
}
function itemClicked(event:MouseEvent):void {
if (event.target is Item) {
var item:Item = Item(event.target);
TweenLite.to(item.btn_delete, 1,{rotation:180});
setTimeout(delayedFunction,5000);
item.parent.removeChild(item);
parseXML(data);
}
}
function delayedFunction():void {
var item:Item = Item(event.target);
item.parent.removeChild(item);
}
(I've mocked up the functions above, so I may be missing some closing parenthesis, but this doesn't matter in relation to my question)
This question has been on my mind for days and I can't think of a way of making the event.target of the parent _item accessible to functions other than the listening function.
You can pass arguments to your setTimeout call like this:
setTimeout(delayedFunction, 5000, event.currentTarget);
Then get the argument from the arguments array from your delayedFunction method:
function delayedFunction():void {
var item:Item = arguments[0] as Item;
item.parent.removeChild(item);
}
Have a look at the setTimeout docs if you need information.

unload external swf (child) from the main screen when it has reached last frame?

I have an external swf loading into a main frame (the URL request), and when the swf reaches it's final frame I need it to UNLOAD itself. I need to do this without any code on the CHILD swf, as this is for an iOS application. Can anyone help?
//start button
start_button_aboriginal.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_3);
import fl.display.ProLoader;
var fl_ProLoader_3:ProLoader;
//This variable keeps track of whether you want to load or unload the SWF
var fl_ToLoad_3:Boolean = true;
function fl_ClickToLoadUnloadSWF_3(event:MouseEvent):void
{
if(fl_ToLoad_3)
{
fl_ProLoader_3 = new ProLoader();
fl_ProLoader_3.load(new URLRequest("myths/myth_aboriginal.swf"));
addChild(fl_ProLoader_3);
fl_ProLoader_3.x = 114;
fl_ProLoader_3.y = 41;
}
else
{
fl_ProLoader_3.unload();
removeChild(fl_ProLoader_3);
fl_ProLoader_3 = null;
}
// Toggle whether you want to load or unload the SWF
fl_ToLoad_3 = !fl_ToLoad_3;
//here, I want to UNLOAD the external SWF when it is finished playing.
var totFrames:Number=childMC.totalFrames;
var curFrame:Number;
childMC.addEventListener(Event.ENTER_FRAME, remove);
function remove(evt:Event):void {
curFrame=childMC.currentFrame;
if (totFrames==curFrame) {
removeChild(childMC);
}
}
You need to declare childMC in a global scope and the assign the loader content.
and dont declare functions inside of functions!!
something like this NOT TESTET
import fl.display.Loader; // impoerts belong at the top
var fl_ProLoader_3:ProLoader; // then your global vars
var childMC:MovieClip; // instatiate childMC with global scope
start_button_aboriginal.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_3,false,0,false); // listener with weak refference
var fl_ToLoad_3:Boolean = true;
function fl_ClickToLoadUnloadSWF_3(event:MouseEvent):void
{
if(fl_ToLoad_3)
{
fl_ProLoader_3 = new Loader();
var url:URLRequest = new URLRequest("myths/myth_aboriginal.swf");
var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null); // IOS needs this
fl_ProLoader_3.load(url, loaderContext);
fl_ProLoader_3.addEventListener(Event.COMPLETE, loadCompleteHandler,false,0,false);
}
else
{
if(childMC){
removeChild(childMC);
childMC.unloadAndStop();
childMC = null;
}
}
// Toggle whether you want to load or unload the SWF
fl_ToLoad_3 = !fl_ToLoad_3;
}
function loadCompleteHandler(evt:Event):void
{
childMC = evt.target.content as MovieClip;
childMC.addEventListener(Event.ENTER_FRAME, remove);
addChild(childMC);
childMC.x = 114;
childMC.y = 41;
}
function remove(evt:Event):void {
var totFrames:Number=childMC.totalFrames;
var curFrame:Number =childMC.currentFrame;;
if (totFrames==curFrame) {
childMC.removeEventListener(Event.ENTER_FRAME, remove);
removeChild(childMC);
childMC.unloadAndStop();
childMC = null;
}
}

action script 3.0 .I had tried a program that should move the dragger as well as the content movieclip,

In this code i need a progressbar to progress through while the imported .swf file is playing, meanwhile i should able to drag the dragger in the progress bar (i.e the rate of movement of dragger should sync with rate of .swf file playing). I got Argument error: #2109 Frame label 459.99 not found in scene1.
var loader:Loader = new Loader();
playBtn.visible = true;
pauseBtn.visible = false;
btn_00.addEventListener(MouseEvent.CLICK, fileLoaded);
btn_01.addEventListener(MouseEvent.CLICK, fileLoaded);
btn_02.addEventListener(MouseEvent.CLICK, fileLoaded);
function fileLoaded(evt:MouseEvent):void
{
var fileName:String = evt.currentTarget.name;
var fileNumber:String = fileName.split("_")[1];
var urlPath:String = "assets/file_" + fileNumber + ".swf";
loader.load(new URLRequest(urlPath));
addChild(loader);
}
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
function swfLoaded(event:Event):void
{
addEventListener(Event.ENTER_FRAME, trackPlayback);
}
function trackPlayback(event:Event):void
{
var perPlayed:Number = MovieClip(loader.content).currentFrame / MovieClip(loader.content).totalFrames;
progressbar.drag.x = (progressbar.bar.width - progressbar.drag.width) * perPlayed;
}
progressbar.drag.buttonMode = true;
var dragClicked:Boolean = false;
var xpos:Number = progressbar.bar.x * progressbar.drag.width;
progressbar.drag.addEventListener(MouseEvent.MOUSE_DOWN,dragMouseDown);
function dragMouseDown(evt:MouseEvent):void
{
trace(" inside mouse down ");
dragClicked = true;
progressbar.drag.startDrag(false,new Rectangle(xpos,0,progressbar.width-progressbar.drag.width,0));
}
progressbar.drag.addEventListener(MouseEvent.MOUSE_UP,dragMouseUp);
function dragMouseUp(evt:MouseEvent):void
{
dragClicked = false;
progressbar.drag.stopDrag();
var cnt:Number = (progressbar.drag.x/(progressbar.width-progressbar.drag.width))*MovieClip(loader.content).totalFrames;
MovieClip(loader.content).gotoAndPlay(cnt);
}
Pls solve my issue.
Thanks in advance.
To access a specific frame, you need to provide an integer value where at the moment you are using a floating-point value.
A simple fix would be to cast the Number to an int:
MovieClip(loader.content).gotoAndPlay(int(cnt));

use 1 object multiple times in as3?

I'm trying to make something like bookmarks, I have 1 note on the stage and when the user clicks it, it starts to drag and the users drops it where they want. the problem is I want these notes to be dragged multiple times.. here is my code:
import flash.events.MouseEvent;
//notess is the instance name of the movie clip on the stage
notess.inputText.visible = false;
//delet is a delete button inside the movie clip,
notess.delet.visible = false;
//the class of the object i want to drag
var note:notes = new notes ;
notess.addEventListener(MouseEvent.CLICK , newNote);
function newNote(e:MouseEvent):void
{
for (var i:Number = 1; i<10; i++)
{
addChild(note);
//inpuText is a text field in notess movie clip
note.inputText.visible = false;
note.x = mouseX;
note.y = mouseY;
note.addEventListener( MouseEvent.MOUSE_DOWN , drag);
note.addEventListener( MouseEvent.MOUSE_UP , drop);
note.delet.addEventListener( MouseEvent.CLICK , delet);
}
}
function drag(e:MouseEvent):void
{
note.startDrag();
}
function drop(e:MouseEvent):void
{
e.currentTarget.stopDrag();
note.inputText.visible = true;
note.delet.visible = true;
}
function delet(e:MouseEvent):void
{
removeChild(note);
}
any help will be appreciated.
You need to create a new instance of your note class when you drop, copy the location and other variables from the note you were dragging, add your new note to the stage, and return the dragging note to its original position.
Something like:
function drop($e:MouseEvent):void
{
$e.currentTarget.stopDrag();
dropNote($e.currentTarget as Note);
}
var newNote:Note;
function dropNote($note:Note):void
{
newNote = new Note();
// Copy vars:
newNote.x = $note.x;
newNote.y = $note.y;
// etc.
// restore original note.
// You will need to store its original position before you begin dragging:
$note.x = $note.originalX;
$note.y = $note.orgiinalY;
// etc.
// Finally, add your new note to the stage:
addChild(newNote);
}
... this is pseudo-code really, since I don't know if you need to add the new note to a list, or link it to its original note. If you Google ActionScript Drag Drop Duplicate, you will find quite a few more examples.
I think you are not target the drag object in drag function and problem in object instantiation
for (var i:Number = 1; i<numberOfNodes; i++) {
note = new note();
addChild(note);
...
....
}
function drag(e:MouseEvent):void{
(e.target).startDrag();
}
If you are dragging around multiple types of objects (eg. Notes and Images), you could do something like this, rather than hard coding the type of object to be instantiated.
function drop(e:MouseEvent):void{
// Get a reference to the class of the dragged object
var className:String = flash.utils.getQualifiedClassName(e.currentTarget);
var TheClass:Class = flash.utils.getDefinitionByName(className) as Class;
var scope:DisplayObjectContainer = this; // The Drop Target
// Convert the position of the dragged clip to local coordinates
var position:Point = scope.globalToLocal( DisplayObject(e.currentTarget).localToGlobal() );
// Create a new instance of the dragged object
var instance:DisplayObject = new TheClass();
instance.x = position.x;
instance.y = position.y;
scope.addChild(instance);
}