AS3 stop function then add back to menu button - actionscript-3

Hello im new in AS3 and i have this simple game project but i cant make back to menu button . can someone help me ?
the project have 3 frames , frame 1=title frame2= menu , frame 3 = game
and i want to go back to frame 1 when the game hit gameover .
but when i go back to frame 1 with GotoandStop , it always show error message like :
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at next_fla::MainTimeline/BintangGo()[next_fla.MainTimeline::frame3:88]
or is there a way to reset everything and play again at frame 1 ?
this is my code at frame 3
import flash.events.Event;
game_over.visible=false;
men.visible=false;
var scoree:uint;
tong.addEventListener(Event.ENTER_FRAME,meteorGo)
function meteorGo(ev:Event){
tong.y+=15
tong.rotation+=7
if(tong.y>=810){
tong.y=0-Math.random()*800
tong.x=Math.random()*480
}
if(tong.hitTestObject(wong)){
game_over.visible=true;
men.visible=true;
wong.x=1600-Math.random()*400
}
}
import flash.events.Event;
apel.addEventListener(Event.ENTER_FRAME,apelgo)
apel.y=0-Math.random()*800
apel.x=Math.random()*480
function apelgo(ev:Event){
apel.y+=11
if(apel.y>=810){
apel.y=0-Math.random()*800
apel.x=Math.random()*480
}
if(apel.hitTestObject(wong)){
updateScore5();
apel.y=0-Math.random()*800
apel.x=Math.random()*480
}
}
botol.addEventListener(Event.ENTER_FRAME,botolgo)
botol.y=0-Math.random()*800
botol.x=Math.random()*480
function botolgo(ev:Event){
botol.y+=12
if(botol.y>=810){
botol.y=0-Math.random()*800
botol.x=Math.random()*480
}
if(botol.hitTestObject(wong)){
updateScore2();
botol.y=0-Math.random()*800
botol.x=Math.random()*480
}
}
bintang.addEventListener(Event.ENTER_FRAME,BintangGo)
bintang.y=0-Math.random()*800
bintang.x=Math.random()*480
function BintangGo(ev:Event){
bintang.y+=9
if(bintang.y>=810){
bintang.y=0-Math.random()*800
bintang.x=Math.random()*480
}
if(bintang.hitTestObject(wong)){
updateScore();
bintang.y=0-Math.random()*800
bintang.x=Math.random()*480
}
}
function init():void
{
scoree = 0;
scorecounter.text = scoree.toString();
}
function updateScore5():void
{
scoree += 5;
scorecounter.text = scoree.toString();
}
function updateScore2():void
{
scoree += 2;
scorecounter.text = scoree.toString();
}
function updateScore():void
{
scoree += 1;
scorecounter.text = scoree.toString();
}
bintanga.addEventListener(Event.ENTER_FRAME,BintangGoa)
bintanga.y=0-Math.random()*800
bintanga.x=Math.random()*480
function BintangGoa(ev:Event){
bintanga.y+=9
if(bintanga.y>=810){
bintanga.y=0-Math.random()*800
bintanga.x=Math.random()*480
}
if(bintanga.hitTestObject(wong)){
updateScore();
bintanga.y=0-Math.random()*800
bintanga.x=Math.random()*480
}
}
bintangb.addEventListener(Event.ENTER_FRAME,BintangGob)
bintangb.y=0-Math.random()*800
bintangb.x=Math.random()*480
function BintangGob(ev:Event){
bintangb.y+=2
if(bintangb.y>=810){
bintangb.y=0-Math.random()*800
bintangb.x=Math.random()*480
}
if(bintangb.hitTestObject(wong)){
updateScore();
bintangb.y=0-Math.random()*800
bintangb.x=Math.random()*480
}
}
bintangc.addEventListener(Event.ENTER_FRAME,BintangGoc)
bintangc.y=0-Math.random()*800
bintangc.x=Math.random()*480
function BintangGoc(ev:Event){
bintangc.y+=9
if(bintangc.y>=810){
bintangc.y=0-Math.random()*800
bintangc.x=Math.random()*480
}
if(bintangc.hitTestObject(wong)){
updateScore();
bintangc.y=0-Math.random()*800
bintangc.x=Math.random()*480
}
}
bintangd.addEventListener(Event.ENTER_FRAME,BintangGod)
bintangd.y=0-Math.random()*800
bintangd.x=Math.random()*480
function BintangGod(ev:Event){
bintang.y+=9
if(bintangd.y>=810){
bintangd.y=0-Math.random()*800
bintangd.x=Math.random()*480
}
if(bintangd.hitTestObject(wong)){
updateScore();
bintangd.y=0-Math.random()*800
bintangd.x=Math.random()*480
}
}
mobil.addEventListener(Event.ENTER_FRAME,MobilGO)
mobil.y=-1600-Math.random()*800
mobil.x=Math.random()*480
function MobilGO(ev:Event){
mobil.y+=20
if(mobil.y>=810){
mobil.y=-1600-Math.random()*800
mobil.x=Math.random()*480
}
if(mobil.hitTestObject(wong)){
game_over.visible=true;
men.visible=true;
wong.x=1600-Math.random()*400
}
}
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
men.addEventListener(TouchEvent.TOUCH_TAP, m12);
function m12(event:TouchEvent):void
{
removeEventListener(Event.ENTER_FRAME,MobilGO);
removeEventListener(Event.ENTER_FRAME,meteorGo);
removeEventListener(Event.ENTER_FRAME,apelgo);
removeEventListener(Event.ENTER_FRAME,botolgo);
removeEventListener(Event.ENTER_FRAME,BintangGoa);
removeEventListener(Event.ENTER_FRAME,BintangGob);
removeEventListener(Event.ENTER_FRAME,BintangGoc);
removeEventListener(Event.ENTER_FRAME,BintangGod);
removeEventListener(Event.ENTER_FRAME,BintangGo);
removeChild (tong);
removeChild (botol);
removeChild (apel);
removeChild (bintang);
removeChild (wong);
removeChild (bintanga);
removeChild (bintangb);
removeChild (bintangc);
removeChild (bintangd);
removeChild (mobil);
gotoAndStop(1);
}
stop();
tyvm if u can help me

On both frame 1 and frame 2 of your main timeline, try adding stop(); as the very first thing.
Judging by the error message mentioning "frame 3" I am guessing it is trying to go to frame 3, and so explicitly calling stop() as soon as possible might prevent that.

Related

as3 flash gotoAndPlay

I am making a small platformer game at flash and I just added the first enemy.
I get this in the "output":
" TypeError: Error #1006: gotoAndPlay is not a function.
at scratch_theGame_nojumping_tryEnemy_fla::MainTimeline/fl_enemyAlerted() "
My code on the enemy layer is the following:
var enemyAlerted:Boolean = false;
var alerted:Boolean = false;
var enemy:Object = new Object();
stage.addEventListener(Event.ENTER_FRAME, fl_alerted);
stage.addEventListener(Event.ENTER_FRAME, fl_enemyAlerted);
function fl_alerted(event:Event):void
{
if (char.x > 400)
{
alerted = true;
}
}
function fl_enemyAlerted(event:Event):void
{
if (alerted = false)
{
enemy.gotoAndPlay("alert");
}
if (alerted = true)
{
enemy.gotoAndPlay("chase");
}
}
My goal here is to make the enemy loop the 'alert' animation from the start until my character has come close enough to be noticed, and then the enemy should play the 'chase' animation. Can anyone help? Thank you in advance.

Flash as3 platform game Error 1009

Im making a platform game with patrolling enemies with an enemy class and a bumper class which changes the direction of the enemies when they collide. Once you open the door and call on the nextLevel function I need the enemies and bumpers to be removed. This is the code for the nextLevel function:
function nextLevel():void {
currentLevel++;
trace("Next Level: " + currentLevel);
if (enemyList.length>0) {
for (var i:int = 0; i < enemyList.length; i++) {
trace("enemyRemoved");
enemyList[i].removeSelf();
}
}
if (bumperList.length>0) {
for (var b:int = 0; b < bumperList.length; b++) {
trace("bumperRemoved");
bumperList[b].removeSelf();
}
}
if (currentLevel==2) {
gotoLevel2();
addEnemiesToLevel2();
addBumpersToLevel2();
}
}
if (currentLevel==3) {
gotoLevel3();
addEnemiesToLevel3();
addBumpersToLevel3();
trace("gotoLevel3");
}
which works fine for going to level 2. But somehow when I go to level 3, I get error 1009 cannot access a property of a null object reference when removing the bumpers. I don't understand why it would work for level 2 but not 3 and also the enemies get removed just fine which is almost identical to the remove bumper code. Here is my removeSelf function in the bumper class
public function removeSelf():void {
trace("remove bumper");
removeEventListener(Event.ENTER_FRAME, bumperloop);
this.parent.removeChild(this);
Also my function for pressing the down arrow key while on the open door to call the nextLevel function
} else if (e.keyCode == Keyboard.DOWN) {
downPressed=true;
if (doorOpen&&player.hitTestObject(back.other.lockedDoor)) {
nextLevel();
}
Anyone know what could be the problem here? I would be very grateful to anyone who could answer Iv'e been working at this for days. It gives me an error on the bumperList[b].removeSelf(); line on the nextLevel function and this.parent.removeChild(this); in the bumper class and on the nextLevel(); line in my keydown code

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.

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.

Error #2007: Parameter hitTestObject must be non-null

this is my code- it's working and moving to frame 3 but everything is stuck there and I
get this Error #2007
function createMC(event:Event):void
{
var hasa_mc:MovieClip= new hasa();
stage.addChild(hasa_mc);
var halfMc:int=hasa_mc.width/2;
hasa_mc.x=randomNum(70+halfMc,480-halfMc);
hasa_mc.addEventListener(Event.ENTER_FRAME, abc);
hasa_mc.addEventListener(Event.ENTER_FRAME, dropCheckHit);
function dropCheckHit(event:Event):void
{
if (hasa_mc.hitTestObject(hauta1_mc)) {
hasa_mc.removeEventListener(Event.ENTER_FRAME, dropCheckHit);
event.target.parent.removeChild(event.target);
countertime++;
score_txt.text=String(countertime*10)
if (countertime==10)
{
gotoAndStop(3);
}
The error code and description refers to the line:
if (hasa_mc.hitTestObject(hauta1_mc)) {
My guess would be the movieclip hauta1_mc does not exist on frame 3 of your movie, so once you go to frame 3 and your dropCheckHit function executes the null reference error is thrown.
To resolve you can remove the enter frame listener and stop checking if the movieclip has been hit:
if (countertime==10)
{
hasa_mc.removeEventListener(Event.ENTER_FRAME, dropCheckHit);
gotoAndStop(3);
}
Note: You may need to remove the other listener you have on hasa_mc as well if hasa_mc does not exist in frame 3.
thank you. from where i need to remove also ? still does not works.
when it goes to frame 3 there is new MC that hit new object
function dropCheckHit(event:Event):void {
if (hasa_mc.hitTestObject(hauta1_mc)) {
hasa_mc.removeEventListener(Event.ENTER_FRAME, dropCheckHit);
event.target.parent.removeChild(event.target);
countertime++;
score_txt.text=String(countertime*10)
if (countertime==10)
{
gotoAndStop(3);
hasa_mc.removeEventListener(Event.ENTER_FRAME, dropCheckHit);
}
hauta1_mc.nextFrame();}
else if (hasa_mc.y > 380)
{
xdirection = 0;
ydirection = 0;
hasa_mc.x = 190;
hasa_mc.y = 200;
hauta1_mc.x=220;
lifeCounter--; //
life_txt.text=String(lifeCounter);
trace(lifeCounter);
if (lifeCounter==0)
{
gotoAndStop(5);
}
}
}
}