Why does TweenLite.to and hitTestObject not work? - actionscript-3

this is my code:
stop();
import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.TweenMax;
import com.greensock.TweenLite;
import flash.events.Event;
import com.greensock.TweenLite
stage.addEventListener(MouseEvent.CLICK, rijden); // Add the button click
function rijden(e:MouseEvent):void {
TweenLite.to(auto, 4, {x:666.15, y:375.6});
}
addEventListener(Event.ENTER_FRAME, einde1);
function einde1(e:Event){
if(auto.hitTestObject(stopauto)){
var myTween=TweenLite.to(auto, 4, {x:666.15, y:375.6});
myTween.kill(); //here code for tween killing
trace("works")
//
auto.x = 241;
auto.y = 375;
removeEventListener(Event.ENTER_FRAME, einde1)
}
}
and I want that if auto hits stopauto, auto goes to
auto.x = 241;
auto.y = 375;
it does the trace but it doesn't go to the x and y what I want it to go

You have two tweens, one of them is never killed. I guess it simple goes on and overrides your x/y for the final car destination.
//tween one
function rijden(e:MouseEvent):void {
TweenLite.to(auto, 4, {x:666.15, y:375.6});
}
//tween two
function einde1(e:Event){
if(auto.hitTestObject(stopauto)){
var myTween=TweenLite.to(auto, 4, {x:666.15, y:375.6});
myTween.kill(); //here code for tween killing
//REST OF YOUR CODE
}
}

#Fygo is absolutely correct. In your enterframe handler, instead of getting a reference to the Tween you started in the click handler, you are in fact creating a new Tween which you then kill immediately, rather than the original Tween which continues to execute.
I think the following will fix it:
// ... Rest of your code
function rijden(e:MouseEvent):void {
// start the tween on mouse click
TweenLite.to(auto, 4, {x:666.15, y:375.6});
}
addEventListener(Event.ENTER_FRAME, einde1);
function einde1(e:Event){
if(auto.hitTestObject(stopauto)){
// Objects have collided so stop the tween
TweenLite.killTweensOf(auto);
// Place the object somewhere else
auto.x = 241;
auto.y = 375;
removeEventListener(Event.ENTER_FRAME, einde1)
}
}

Related

How to remove a tween before moving to next frame in as3

I have a movieclip which spins. I want it when users drag and drop it to stop spinning and be in it's initial position. I wrote this code but i get error TypeError: Error #1009: Cannot access a property or method of a null object reference.
at omoixes10_fla::MainTimeline/EntFrame() when I move to next frame. I can not see what i did wrong. Can you please help me with my code? Do I have to remove tween before I move to next frame?
import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
tick1.parent.removeChild(tick1);
wrong1.parent.removeChild(wrong1);
sentences2.buttonMode=true;
sentences1.buttonMode=true;
Piece1_mc.buttonMode=true;
var my_x:int=stage.stageWidth
var my_y:int=stage.stageHeight
var myWidth:int=0-my_x;
var myHeight:int=0-my_y;
var boundArea:Rectangle=new Rectangle(my_x, my_y, myWidth ,myHeight);
var spin:Tween=new Tween(Piece1_mc, "rotation",Elastic.easeInOut,0,360,5,true);
spin.stop();
sentences2.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
snoopy.gotoAndPlay(2);
addChild(tick1);
addChild(wrong1);
sentences2.removeEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
sentences1.removeEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);
spin.start();
spin.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
function onFinish(e:TweenEvent):void {
e.target.yoyo();
{
Piece1_mc.addEventListener(MouseEvent.MOUSE_DOWN, DragP1);
function DragP1 (event:MouseEvent):void
{
Piece1_mc.startDrag();
Piece1_mc.startDrag(false,boundArea);
spin.stop();
}
stage.addEventListener(MouseEvent.MOUSE_UP, DropP1);
function DropP1(event:MouseEvent):void
{
Piece1_mc.stopDrag();
}
if(Targ1_mc.hitTestObject(Piece1_mc.Tar1_mc)) {
Piece1_mc.x=677;
Piece1_mc.y=48,10;
myTimer.start();
spin.stop();
}
}
}
sentences1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);
function fl_MouseClickHandler_2(event:MouseEvent):void
{
snoopy.gotoAndPlay(64);
}
var myTimer:Timer = new Timer(2000,1);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void {
gotoAndStop(15);
if (Piece1_mc.parent)
{
Piece1_mc.parent.removeChild(Piece1_mc);
}if (tick1.parent)
{
tick1.parent.removeChild(tick1);
}
if (wrong1.parent)
{
wrong1.parent.removeChild(wrong1);
}
}
}
So the first problem was adding your event listeners inside an enter frame which was not the right way to do since it will keep on adding event listeners at every frame.
Second, you should use a mouse move event listener as I've recommended before to track and test your hit test.
Thirdly, since you are rotating your MovieClip, and you want it to go back to its initial state, you should do :
Piece1_mc.rotation=0;
Hope this helps.

Tween event listener

In a frame script I have an event listener that should call the 'onFinish' function when the tween motion is finished. (See pertinent parts in red below.) I never see that 'done tweening' output. It fails silently. I've tried the addEventListener in different places.. to no avail. What am I missing?
Thanks!
import fl.transitions.*;
import fl.transitions.easing.*;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
var currentFrameMC = animImg;
var scaleXTween:Tween=new Tween(animImg,"scaleX",Bounce.easeOut, 1,2,2.4,true);
var scaleYTween:Tween=new Tween(animImg,"scaleY",Bounce.easeOut, 1,2,2.4,true);
var alphaTween:Tween = new Tween(animImg, "alpha", Strong.easeOut, .5, 1, 11, true);
//Put a listener on the MC so I can tell when it's done tweening the scale.
currentFrameMC.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
//This is another event listner put on a button:
//(The button, when clicked, will trigger the shrinking of the animImg MC)
reverseTween1.addEventListener(MouseEvent.CLICK,shrinkFrameMC);
//Shrink/scale down the anImg by tweening
function shrinkFrameMC(e:MouseEvent) //This scales down the playing movie clip
{
scaleXTween=new Tween(currentFrameMC,"scaleX",None.easeNone, currentFrameMC.scaleX,1,3,true);
scaleYTween=new Tween(currentFrameMC,"scaleY",None.easeNone, currentFrameMC.scaleY,1,3,true);
//Tween the alpha state of the movie clip again, this time in reverse
alphaTween=new Tween(currentFrameMC, "alpha", Strong.easeOut, 1, .5, 11, true);
}
function onFinish(e:TweenEvent):void //This does an action when the frame MC is done tweening
{
trace ("done tweening" );
//NEVER SEE THIS OUTPUT
}
You need to assign the Tween event listener to the Tween, not the object being tweened.
its working:
import fl.transitions.*;
import fl.transitions.easing.*;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
var scaleXTween:Tween=new Tween(animImg,"scaleX",Bounce.easeOut, 1,2,2.4,true);
var scaleYTween:Tween=new Tween(animImg,"scaleY",Bounce.easeOut, 1,2,2.4,true);
var alphaTween:Tween = new Tween(animImg, "alpha", Strong.easeOut, .5, 1, 11, true);
//Put a listener on the MC so I can tell when it's done tweening the scale.
scaleXTween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
//This is another event listner put on a button:
//(The button, when clicked, will trigger the shrinking of the animImg MC)
reverseTween1.addEventListener(MouseEvent.CLICK,shrinkFrameMC);
//Shrink/scale down the anImg by tweening
function shrinkFrameMC(e:MouseEvent) //This scales down the playing movie clip
{
scaleXTween=new Tween(currentFrameMC,"scaleX",None.easeNone, currentFrameMC.scaleX,1,3,true);
scaleYTween=new Tween(currentFrameMC,"scaleY",None.easeNone, currentFrameMC.scaleY,1,3,true);
//Tween the alpha state of the movie clip again, this time in reverse
alphaTween=new Tween(currentFrameMC, "alpha", Strong.easeOut, 1, .5, 11, true);
}
function onFinish(e:TweenEvent):void //This does an action when the frame MC is done tweening
{
trace ("done tweening" );
//NEVER SEE THIS OUTPUT
}

making a symbol move by keyboard not showing result and when published it not reads stop(); but replays it again and again

I am new to actionscript ,
My document class is ,
package
{
//list of our imports these are classes we need in order to
//run our application.
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class engine extends MovieClip
{
// moved ourShip to a class variable.
private var Circle:circle = new circle()
//our constructor function. This runs when an object of
//the class is created
public function engine()
{
addFrameScript(0, frame1);
addFrameScript(1, frame2);
}
// frame 1 layer 1 --------------------------------------------------
public function frame1()
{
stop();
}
//-------------------------------------------------------------------
// frame 2 layer 1 --------------------------------------------------
public function frame2()
{
Circle.x = stage.stageWidth / 2;
Circle.y = stage.stageHeight / 2;
addChild(Circle);
}
//-------------------------------------------------------------------
}
}
i made two frames first contains button and the other circle which i want to move but it not moves and it stays in the middle on second frame
My button class is
package
{
//imports
import flash.events.MouseEvent;
import flash.display.SimpleButton;
import flash.display.MovieClip;
//-------
public class start extends SimpleButton
{
public function start()
{
addEventListener(MouseEvent.CLICK, onTopClick);
addEventListener(MouseEvent.MOUSE_OVER, onBottomOver);
}
function onTopClick(e:MouseEvent):void
{
MovieClip(root).gotoAndStop(2)
}
function onBottomOver(e:MouseEvent):void
{
}
}
}
And my as of circle movieclip is
package
{
//imports
import flash.display.MovieClip;
import flash.display.Stage;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class circle extends MovieClip
{
private var speed:Number = 0.5;
private var vx:Number = 0;
private var vy:Number = 0;
private var friction:Number = 0.93;
private var maxspeed:Number = 8;
public function circle()
{
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
x+=vx;
y+=vy
}
function keyHit(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.RIGHT :
vx+=speed;
break;
case Keyboard.LEFT :
vx-=speed;
break;
case Keyboard.UP :
vy-=speed;
break;
case Keyboard.DOWN :
vy+=speed;
break;
}
}
}
}
I am sorry to post so much for you guys to read but stackoverflow is the only website where anyone helps me !
You have made several major errors. First, addFrameScript() isn't a proper way to place code on frames, use Flash's editor to place code on timeline. (IIRC you will have to make a single call out of your two in order to have all the code you add to function) And, whatever code you added to a frame of a MC is executed each frame if the MC's currentFrame is the frame with code. Thus, you are adding a function "frame2()" that places the Circle in the center of the stage each frame! You should instead place it at design time (link it to a property) into the second frame, or in a constructor, or you can use one single frame and Sprite instead of MovieClip, and instead of using frames you can use container sprites, adding and removing them at will, or at an action.
The other major mistake is adding an event listener inside an enterframe listener - these accumulate, not overwrite each other, so you can have multiple functions be designated as listeners for a particular event, or even one function several times. The latter happens for you, so each frame another instance of a listening keyHit function is added as a listener. The proper way to assign listeners is either in constructor, or in any function that listens for manually triggered event (say, MouseEvent.CLICK), but then you have to take precautions about listening for more than once with each function, and listening only with those functions you need right now.
EDIT:
Okay. Your code was:
addFrameScript(0, frame1);
addFrameScript(1, frame2);
The more correct way should be:
addFrameScript(0,frame1,1,frame2);
The reason is, the call to addFrameScript replaces all the timeline code with what you supply within here. The function is undocumented, perhaps by the reason of its affects on the stage and AS3 environment. The closest thing to the documentation on addFrameScript() so far is this link.
Next: Your code is:
public function circle()
{
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
x+=vx;
y+=vy
}
The correct way of writing this is as follows:
public function circle()
{
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(e:Event=null):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
}
public function loop(e:Event) : void
{
x+=vx;
y+=vy
}
The listeners should be assigned in constructor, if they are permanent or you want them to be active as soon as you create an object. The KeyboardEvent listeners are separate case, as in order for them to function you have to assign them to stage, which is not available right at the time of creating the object, so you need an intermediate layer - the init() function, that is only called when the object is added to stage. At this point stage is no longer null, and you can assign an event listener there. Note, if you want to make your circles eventually disappear, you have to remove the listener you assigned to stage at some point of your removal handling code.
Next: Your code:
public function frame2()
{
Circle.x = stage.stageWidth / 2;
Circle.y = stage.stageHeight / 2;
addChild(Circle);
}
Correct code should be:
public function frame2():void
{
if (Circle.parent) return; // we have added Circle to stage already!
Circle.x = stage.stageWidth / 2;
Circle.y = stage.stageHeight / 2;
addChild(Circle);
}
See, you are calling this every time your MC is stopped at second frame, thus you constantly reset Circle's coordinates to stage center, so you just cannot see if it moves (it doesn't, as you have assigned the keyboard listener not to stage).
Perhaps there are more mistakes, but fixing these will make your MC tick a little bit.

Timer stops working properly after a few seconds

I have a tween playing every few seconds, and it works fine in the beginning, but then it starts to get jittery and resets before it even gets halfway through the tween.
Any idea why? the timer also seems to have a longer wait the first time i run the animation
import flash.events.Event;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.utils.Timer;
import flash.events.TimerEvent;
addEventListener(Event.ENTER_FRAME, move);
var signalTimer:Timer = new Timer(3000, 0);
function move(e:Event){
sender.x = mouseX;
sender.y = mouseY;
signalTimer.addEventListener(TimerEvent.TIMER, sendSignal);
signalTimer.start();
}
function sendSignal(e:TimerEvent){
signalTimer.stop();
var sigTween1X:Tween = new Tween(signal1, "x", None.easeOut, sender.x, mic1.x, 10, false);
var sigTween1Y:Tween = new Tween(signal1, "y", None.easeIn, sender.y, mic1.y, 15, false);
var sigTween3X:Tween = new Tween(signal3, "x", Strong.easeIn, sender.x, mic3.x, 7, true);
var sigTween3Y:Tween = new Tween(signal3, "y", Strong.easeOut, sender.y, mic3.y, 7, true);
}
This is a heavy segment of code; combining frame-based, timer, and tween timings.
Currently each frame calls move() which adds an event listener to your signalTimer without removing the previously added event listener:
signalTimer.removeEventListener(TimerEvent.TIMER, sendSignal);
However, adding and removing an event listener per frame is not optimal.
If you really want your timer to fire every 3-seconds, instantiate and add an event listener upon instantiation (not within the move() function):
var signalTimer:Timer = new Timer(3000, 0);
signalTimer.addEventListener(TimerEvent.TIMER, sendSignal);
signalTimer.start();
Then don't stop the timer in sendSignal().
Addressing the tweens, you could implement frame-based animation such as:
signal1.x -= (signal1.x - mic1.x) * 0.9;
signal1.y -= (signal1.y - mic1.y) * 0.9;
Therefore, implementing a delta based function to track x,y coordinates.

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller

I tempt to change my code with your instructions but don't work :/
import flash.ui.Keyboard;
import flash.events.Event;
import flash.sampler.NewObjectSample;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.media.SoundChannel;
//stage.addEventListener(KeyboardEvent.KEY_DOWN,test);
/*function test(e:KeyboardEvent){
trace('code='+e.keyCode);
}
*/
//--- Variables: ---
stop();
var timer1: Timer=new Timer (115000,1);
var timerPoint: Timer=new Timer (115000,0);
var points: Number = 0;
pisteWWRY = lvl1WWRY.play(); // Play the music
stage.addCh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1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1,0,0,0,1,0,0,0,1,0,0,0,1,1,0,0]]; //Arr ay to create level.
var L:int=0;
var C:int=0;
// création du tableau.
function lvl1 () {
for (L=0; L<tableau1.length ;L++){
for(C=0; C<tableau1[0].length ;C++){
if (tableau1[L][C]==1){
var Blockn1: block=new block();
map.addChild(Blockn1);
Blockn1.x=1620+C*60;
Blockn1.y=30+L*50;
Blockn1.addEventListener (Event.ENTER_FRAME, collisions);
stage.removeEventListener (Event.ENTER_FRAME, lvl1 );
}
}
}
}
//--- Ecouteurs: ---
timer1.addEventListener (TimerEvent.TIMER, stoplvl1);
timer1.start();
timerPoint.addEventListener (TimerEvent.TIMER, pointlvl1);
timerPoint.start();
stage.addEventListener (Event.ENTER_FRAME, moove);
//--- Fonctions: ---
function stoplvl1 (e:TimerEvent) :void{
gotoAndStop(4);
}
function pointlvl1 (e:TimerEvent) :void{
points++;
Scorelvl1.text = points.toString();
}
function collisions (e:Event):void {
if(e.target.hitTestObject(Perso)){
if (map && stage.contains(map)) {
stage.removeChild(map);
}
stage.removeEventListener (Event.ENTER_FRAME, moove);
pisteWWRY = lvl1WWRY.stop();
gotoAndStop(4);
}
}
This is my code now.
And my error:
TypeError: Error #1006: stop is not a function.
at projsolo_fla::MainTimeline/collisions()[projsolo_fla.MainTimeline::frame3:78]
I don't go to the frame 4 and if I withdraws : pisteWWRY = lvl1.stop(); i crash when i do collisions ...
In the collisions function you are removing the map from the stage but not stopping the ENTER_FRAME event that calls collisions so the second time it gets called map is no longer a child of the stage and you get the error.
You could do something like this
function collisions (e:Event):void {
if(e.target.hitTestObject(Perso)){
if (map && stage.contains(map)) {
stage.removeChild(map);
}
stage.removeEventListener (Event.ENTER_FRAME, moove);
stage.removeEventListener (Event.ENTER_FRAME, lvl1);
playWWRY.removeEventListener (MouseEvent.CLICK, jouerWWRY);
pisteWWRY = lvl1WWRY.stop();
gotoAndStop(4);
}
}
which will alleviate the problem, but I think you should rethink some of your code logic as that function is going to keep running until you remove the ENTER_FRAME event listener (which you can't even easily access anymore since it is no longer in scope).