How to remove a tween before moving to next frame in as3 - actionscript-3

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.

Related

Labyrinth/maze game

I'm creating a very simple game in Flash AS3 including labyrinth. Here's the code:
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
oseba.addEventListener(Event.ENTER_FRAME, premik);
oseba.addEventListener( Event.ENTER_FRAME, handleCollision)
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
var keys:Array = [];
function keyDownHandler(e:KeyboardEvent):void{
keys[e.keyCode] = true;
}
function keyUpHandler(e:KeyboardEvent):void{
keys[e.keyCode] = false;
}
function premik(e:Event):void{
if (keys[Keyboard.RIGHT]) {
oseba.x += 5;
}
if (keys[Keyboard.LEFT]) {
oseba.x -= 5;
}
if (keys[Keyboard.UP]) {
oseba.y -= 5;
}
if (keys[Keyboard.DOWN]) {
oseba.y += 5;
}
}
function handleCollision(e:Event ):void{
if(oseba.hitTestObject(nazaj)){
gotoAndPlay(2,"igra");
}
if(oseba.hitTestObject(gozd)){
gotoAndPlay(2);
}
I'd like to add collision detection, that will disallow my ''oseba'' that is walking around from walking over unmarked terrain. Below I've created a non visible MC ''potke''. I supposed the best way would be to calculate ''oseba'' 's next position and if it's on ''potke'' then ''oseba'' can't move there. I'm looking for suitable example of code, cos I've tried few different already, but non seems to work.
I'm also receiving the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at XYgame_fla::MainTimeline/handleCollision()
Everything seems to work fine otherwise, but this error keeps showing up.
I would try using nazaj.hitTestPoint(oseba.x, oseba,y, true) and put the EventListener to the stage to fix the error.

How would I play a sound when the collision occurs?

I am new to flash and using as3. I am in the process of making a simple catching game where the items fall from the top and you control a basket at the bottom to catch them. My script is fine and is playing without erros throughout which I am happy about, but how would I add a sound clip to this script to play when the item lands in the basket? Thanks in advance!!!
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.TextField;
var catcher:Catcher;
var createEnemyID:uint;
var gameSpeed:uint;
var droppedText:TextField;
var caughtText:TextField;
var score:uint=0;
function initGame():void{
catcher=new Catcher();
catcher.x=500;
catcher.y=1400;
addChild(catcher);
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveCatcher);
Mouse.hide();
gameSpeed=500;
createEnemyID=setInterval(createEnemy,gameSpeed);
droppedText=new TextField();
droppedText.x=50;
droppedText.y=50;
addChild(droppedText);
caughtText=new TextField();
caughtText.x=250;
caughtText.y=50;
addChild(caughtText);
droppedText.text=caughtText.text='0';
}
function moveCatcher (e:MouseEvent):void{
catcher.x=this.mouseX;
e.updateAfterEvent();
}
function createEnemy():void{
var enemy:Faller=new Faller();
enemy.y=-1;
enemy.x=Math.random()*stage.stageWidth;
enemy.addEventListener (Event.ENTER_FRAME, dropEnemy);
addChild(enemy);
}
function dropEnemy(e:Event):void{
var mc:Faller=Faller(e.target);
mc.y+=15;
if(mc.hitTestObject(catcher)) {
caught(mc);
}
else if (mc.y>stage.stageHeight){
dropped(mc);
}
}
function caught(mc:Faller):void{
mc.removeEventListener (Event.ENTER_FRAME,dropEnemy);
removeChild(mc);
caughtText.text=String(Number(caughtText.text)+1);
}
function dropped(mc:Faller):void{
mc.removeEventListener (Event.ENTER_FRAME,dropEnemy);
removeChild(mc);
droppedText.text=String(Number(droppedText.text)+1);
if(droppedText.text=='5'){
gameOver();
}
}
function gameOver():void{
score=Number(caughtText.text);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveCatcher);
removeChild(catcher);
clearInterval(createEnemyID);
removeChild(caughtText);
removeChild(droppedText);
while(numChildren>0){
getChildAt(0).removeEventListener(Event.ENTER_FRAME,dropEnemy);
removeChildAt(0);
}
Mouse.show();
gotoAndStop('gameover');
}
initGame();
import the sound into flash.
edit the properties and set the class of the sound to MySoundClass or whatever you like but you have to reference it later.
In your code write the following in the collision method.
var sound:Sound = new MySoundClass();
sound.play();
See this AS3 Sound tutorial

error access of undefined event

i cant seem to go back to the first frame after the game over screen.
i get the error message Scene 1, 1119: Access of possibly undefined property Event through a reference with static type Class in frame 2 is it because i don't have a classset up in frame 1.
import flash.events.Event;
stop();
var isRight:Boolean=false
var isLeft:Boolean=false
var isUp:Boolean=false
var isDown:Boolean=false
stage.addEventListener(KeyboardEvent.KEY_DOWN, downKey);
function downKey(event:KeyboardEvent)
{
if(event.keyCode==39)
{
isRight=true
}
if(event.keyCode==37)
{
isLeft=true
}
if(event.keyCode==38)
{
isUp=true
}
if(event.keyCode==40)
{
isDown=true
}
}
ect...and the next frame code to go back to the previous one is
import flash.events.Event;
restart.addEventListener(Mouse.Event.MOUSE_UP,click)
function click(e:MouseEvent)
{
gotoAndStop(1);
}
you accidentally wrote Mouse.Event.MOUSE_UP instead of MouseEvent.MOUSE_UP in your addEventListener function

AS3 Works but I get a ReferenceError: Error #1069 Property startDrag not found

I am trying to make a simple project when you click a button a draggable MovieClip is added to the stag and when you click it releases the MovieClip to the X/Y where you clicked, you can then pickup the MovieClip and drag it into a bin (MovieClip) where it destroys itself. The code is working great I can make multiple Movieclips with the button and they are all destroyed when I drag them in the bin however I don't like having "Error Codes".
import flash.events.MouseEvent;
var rubbish:my_mc = new my_mc();
btntest.addEventListener(MouseEvent.CLICK, makeRubbish);
function makeRubbish (event:MouseEvent):void {
addChild(rubbish);
rubbish.x = mouseX - 10;
rubbish.y = mouseY - 10;
rubbish.width = 50;
this.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
rubbish.buttonMode = true;
}
function stopDragging (event:MouseEvent):void {
rubbish.stopDrag()
event.target.addEventListener(MouseEvent.CLICK, startDragging);
rubbish.buttonMode = true;
if (event.target.hitTestObject(bin))
{
trace("hit");
event.target.name = "rubbish";
removeChild(getChildByName("rubbish"));
}
}
function startDragging (event:MouseEvent):void {
event.target.startDrag();
this.addEventListener(MouseEvent.CLICK, stopDragging);
}
Some Pointers:
The target property of an Event is not always what it seems. It actually refers to the current phase in the event bubbling process. Try using the currentTarget property.
I would also recommend tying the stopDragging method to the stage, as sometimes your mouse won't be over the drag as you're clicking.
I would use the MOUSE_UP event as opposed to a CLICK for standard dragging behaviour.
When dragging, keep a global reference to the drag in order to call the stopDrag method on the correct object.
Try This:
import flash.events.MouseEvent;
var rubbish:my_mc = new my_mc();
var dragging:my_mc;
btntest.addEventListener(MouseEvent.CLICK, makeRubbish);
function makeRubbish (event:MouseEvent):void {
addChild(rubbish);
rubbish.x = mouseX - 10;
rubbish.y = mouseY - 10;
rubbish.width = 50;
rubbish.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
rubbish.buttonMode = true;
}
function stopDragging (event:MouseEvent):void {
this.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging);
if(dragging !== null){
dragging.stopDrag();
if (event.currentTarget.hitTestObject(bin)){
removeChild(dragging);
}
dragging = null;
}
}
function startDragging (event:MouseEvent):void {
dragging = event.currentTarget as my_mc;
dragging.startDrag();
this.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
}

ArgumentError: Error #2025 when removeChild

I wrote a AS3 class to make a Counter function. Click and hold mouse in blue area to define a value. I try to show a indicator picture to users that reminding the variation.
But when I drag mouse a little quick in the blue area a error will occur:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at test/get2()
I have read some similar issue posts, but I can't fix this. Could you give me any help? Thank you!
Download .fla and .as in CS6
Download .fla and .as in CS5
The code is below:
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.text.TextFormat;
import flash.events.Event;
public class test extends Sprite{
var i:int=20; //Set a var for the number displayed in stage.
var mx1:Number; //Set a var to save MouseY when MOUSE_DOWN
var mx2:Number; //Set a var to save MouseY when MOUSE_UP
var num:int=0; //Set a var to calculate result
var sub1:subbar1=new subbar1();
var sub2:subbar2=new subbar2();
var sub3:subbar3=new subbar3();
var add1:addbar1=new addbar1();
var add2:addbar2=new addbar2();
var add3:addbar3=new addbar3();
public function test() {
init1(); //Set TextField and addEventListener
initbar(); //Set indicator picture position when drag mouse
}
private function init1():void{
label=new TextField();
label.text=String(i);
label.width=280;
label.selectable=false;
label.x=140;
label.y=90;
addChild(label);
Controler.addEventListener(MouseEvent.MOUSE_DOWN,get1); //addEventListener to bluearea
}
private function initbar(){
sub1.x=sub2.x=sub3.x=add1.x=add2.x=add3.x=30;
sub1.y=35;
sub2.y=55;
add3.y=sub3.y=75;
add2.y=95;
add1.y=115;
}
private function get1(evt:MouseEvent):void{
mx1=mouseY;
trace(mx1);
Controler.removeEventListener(MouseEvent.MOUSE_DOWN,get1);
stage.addEventListener(MouseEvent.MOUSE_UP,get2); //addEventListener to MOUSE_UP
stage.addEventListener(Event.ENTER_FRAME,lifebar); //add ENTER_FRAME to display indicator picture when move mouse
}
private function get2(evt:MouseEvent):void{
mx2=mouseY;
trace(mx2);
if(mx2<=135&&mx2>=35&&mouseX<=130&&mouseX>=50){ //Limited enable area as the blue area
if(num>=4){ //Set i value depends on num
i=i-3;
}else if(num<=-4){
i=i+3;
}else{
i=i-num;
}
label.text=String(i);
}
if(num==1){ //remove indicator picture when MOUSE_UP
removeChild(sub1);
}
if(num==2){
removeChild(sub1);
removeChild(sub2);
}
if(num>=3){
removeChild(sub1);
removeChild(sub2);
removeChild(sub3);
}
if(num==-1){
removeChild(add1);
}
if(num==-2){
removeChild(add1);
removeChild(add2);
}
if(num<=-3){
removeChild(add1);
removeChild(add2);
removeChild(add3);
}
stage.removeEventListener(MouseEvent.MOUSE_UP,get2);
Controler.addEventListener(MouseEvent.MOUSE_DOWN,get1);
stage.removeEventListener(Event.ENTER_FRAME,lifebar);
}
private function lifebar(evt:Event):void{ //Set a ENTER_FRAME to display indicator picture
num=(mouseY-mx1)/12+1;
trace(num);
if(mouseY!=mx1&&num==1){
addChild(sub1);
}
if(num==2){
addChild(sub2);
}
if(num==3){
addChild(sub3);
}
if(num==-1){
addChild(add1);
}
if(num==-2){
addChild(add2);
}
if(num==-3){
addChild(add3);
}
}
}
}
You are attempting to remove display objects that have not yet been added as a child to the display list.
Even though sub1 has been instantiated, it has not been added at the time you attempt to remove it:
Before calling removeChild(obj) on the display object, first test if it has been added as a child by evaluating whether if(contains(obj)) is true.
At line 67 in test.as, you should perform condition testing to see if sub1 has been added to the display list:
if(num==1) {
if(contains(sub1)) // test to see if sub1 is on the display list
removeChild(sub1);
}
If this issue continues with other children, you should add additional testing in blocks like these:
if(num==2){
removeChild(sub1);
removeChild(sub2);
}