How to gotoAndPlay on mouseover in Adobe Flash AS 3.0 - actionscript-3

In Adobe Flash CC As 3.0 I try to make add gotoAndPlay function on mouseover inside a scene.
I put this code:
this.stop();
this.addChild(overBtn);
this.overBtn.addEventListener("mouseover", function (event)
{
this.gotoAndPlay(17);
});
But its not working, what am I doing wrong?

The syntax for adding an event listener is
<target>.addEventListener(<Event>,<function>);
example
stage.addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
Then you'd have a function elsewhere like this:
private function mouseOver(me:MouseEvent):void{
goToAndPlay(17);
}

for accessing handled object, inside event listener do this
this.stop();
this.addChild(overBtn);
overBtn.addEventListener("mouseover", function (event)
{
event.target.gotoAndPlay(17);
});

Related

Action Script 3.0: Stop Drag with hittestobject

how to stop drag(event) the object with hittestobject.. thanks.
object.addEventListener(TouchEvent.TOUCH_BEGIN, drag);
object.addEventListener(TouchEvent.TOUCH_END,drop);
addEventListener(Event.ENTER_FRAME, loop);
function drag(e:TouchEvent):void {
e.target.startTouchDrag(e.touchPointID);
}
function drop(e:TouchEvent):void {
e.target.stopTouchDrag(e.touchPointID);
}
function loop(e:Event):void {
if (object.hitTestObject(collision)) {
//code to stop drag event?
}
}
or is there other way to stop drag event aside from function drop?
sorry for my bad english.
//edited
In the function drop() e.target is the object that currenty processes the event. In the function loop() you also have some objects. It is not clear which of them is dragging but you should call either object.stopTouchDrag() or collision.stopTouchDrag().
UPDATE
There is an argument for both startTouchDrag and stopTouchDrag functions - touchPointID, it is used to determine what touch point (of many) is processed. When stopping the drag, you need to use the same touchPointID which was used for starting it. When calling the stopTouchDrag from a non-event context, you can't know what touch point it should use. So you need to remember it somehow. If your target object is a MovieClip you can just add a dynamic property to it and save the touchPointID there:
function drag(e:TouchEvent):void {
(e.target as MovieClip).touchPointID = e.touchPointID;
e.target.startTouchDrag(e.touchPointID);
}
function loop(e:Event):void {
if (object.hitTestObject(collision)) {
object.stopTouchDrag(object.touchPointID);
}
}

Flex topLevelApplication and SuperTabNavigator mouse event handler

I am using the flex SuperTabNavigator and want on closing the tab check if the control button was pressed. I tried:
public static var CONTROL_PRESSED:Boolean = false;
public function init():void {
var clickListener:Function = function _clickListener(event:MouseEvent):void{
trace(event.ctrlKey);
if(event.ctrlKey){
CONTROL_PRESSED = true;
}else{
CONTROL_PRESSED = false;
}
};
FlexGlobals.topLevelApplication.addEventListener(MouseEvent.CLICK, clickListener);
}
The problem with this is that the mouse click is called everywhere in the application except on the tab. I also tried the same code but addEventListener(MouseEvent.CLICK, clickListener); to add the listener to the SuperTabNavigator and it did not work at all. Is there another way to catch the mouse click?
This is because the SuperTabNavigator has a private mouse click handler function that hides the MouseEvent:
private function closeClickHandler(event:MouseEvent):void {
if(this.enabled) {
dispatchEvent(new Event(CLOSE_TAB_EVENT));
}
event.stopImmediatePropagation();
event.stopPropagation();
}
You'll need to modify the SuperTab class in the SuperTabNavigator source to dispatch some CustomEvent with the data you want instead of a plain new Event(CLOSE_TAB_EVENT).
Then change the onCloseTabClicked function in SuperTabBar to know about your CustomEvent. Then you can pass that to your application code (by adding it to the SuperTabEvent, perhaps).

play/pause button for flash as3 gallery

I have a slideshow with 4 pictures and runs on a timer. I have a movie clip called play_mc. Inside the movie clip is a button with an instance name of play_btn on keyframe 1. Then on keyframe 2 there is another button with an instance name pause_btn. On the AS3 layer, I have this code:
stop();
play_btn.addEventListener(MouseEvent.CLICK, goToPause);
function goToPause(Event:MouseEvent){
gotoAndStop(2);
}
pause_btn.addEventListener(MouseEvent.CLICK, goToPlay);
function goToPlay(Event:MouseEvent){
gotoAndStop(1);
}
On the main stage on the as3 layer, I have this code (this is not all of the code - all other code works without the play_mc movie clip)
myTimer.addEventListener(TimerEvent.TIMER, autoAdvance);
function autoAdvance(event:TimerEvent){
if(imageNumber<totalImages){
imageNumber++;
}
else(imageNumber = 1);
reload();
}
function reload(){
removeChild(myLoader);
myRequest = new URLRequest(imageNumber + ".jpg");
myLoader.load(myRequest);
addChildAt(myLoader, 1);
}
play_mc.addEventListener(MouseEvent.MOUSE_DOWN, stopTimer);
function stopTimer(event:Event){
myTimer.stop();
}
play_mc.addEventListener(MouseEvent.MOUSE_DOWN, resumeTimer);
function resumeTimer(event:Event){
myTimer.start();
}
I get an error saying:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at gallery_fla::play_Mc_3/frame1()
Basically when I click on the Play button the slideshow starts and the text changes to "Pause" but when I click again, the slideshow does not pause and the text won't change back to "Play".
Anyone have an idea how to help me out here, please??
You've created the pause button eventListener on the frame where the button doesn't exist.
Try placing this
pause_btn.addEventListener(MouseEvent.CLICK, goToPlay);
function goToPlay(Event:MouseEvent){
gotoAndStop(1);
}
on frame two which has the pause button on it.
edit:
instead of creating two listeners on play_mc, just create one and then base the actions on a switch i.e.
var _playToggle:Boolean = true;
play_mc.addEventListener(MouseEvent.MOUSE_DOWN, switchTimer);
function switchTimer(event:Event){
if(_playToggle){
myTimer.start();
}else{
myTimer.stop()
}
_playToggle = !_playToggle;
}

how to call function on movie load in actionscript instead using event listener?

Here is the code to call function using event listener:
var listListener:Object = new Object();
listListener.change = function() { changeImage(); }
thelist.addEventListener("change", listListener);
Is there a way to call the function simply when the frame is loaded using the same function as above?
Thanks
agus
So long as you're calling this from within an instance of a Class that extends EventDispatcher (i.e. MovieClip, Sprite, etc.)...
Use this to dispatch an event of that type (i.e. "change").
dispatchEvent(new Event("change"));

AS3 function running before variables are defined!

I am trying to add an init() function to a MovieClip, but when I run the function from scene1 the variables that were set in the MovieClip are not defined yet... The MovieClip was dragged to the stage from the library.
scene1:
mc.init(null);
MovieClip:
var _default = 5;
function init(num) {
if(num == null) {
trace(_default);
} else {
trace(num);
}
}
This is tracing "undefined" instead of "5"; Is there a way of fixing this problem?
The problem is that any code thats placed directly in the main timeline will always run before the code thats directly in the MovieClip.
The way to get around this would be to let flash finish running that code in both timeline and the MovieClip first, and then call the function from the timeline after its done.
The easiest way to do this would be to use an event listener:
Timeline:
addEventListener( Event.ENTER_FRAME, onEnterFrame );
function onEnterFrame( e:Event ):void {
myObject.init(null);
removeEventListener( Event.ENTER_FRAME, onEnterFrame );
}
That way the timeline will wait until the first frame has started to call the init function of your MovieClip.