How to control movieClip from frame2 but code is frame one - actionscript-3

I need to access movieclip from different frame. Look here movieClip_1 is in frame 1. So it will trace and work properly but movieClip_2 is in frame 2. So it will show this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at mouseclik_fla::MainTimeline/frame1()
But I need to access movieclip_2 from frame 1 coding. I will not write code in frame 2:
stop();
movieClip_1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);
function fl_MouseClickHandler_2(event:MouseEvent):void
{
trace("Mouse clicked");
}
movieClip_2.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
trace("Mouse clicked");
}

Related

stop(); undefined method in as3.0

i want to move to the next frame after clicking a specific button. tried putting stop(); method but it says error and the result is just alternating the 2 frames .
here's my code.
//the error says call to a possibly undefined method stop.
//i'm using adobe flash cc.
stop();
public function main ():void {
enter_button.buttonMode = true;
enter_button.addEventListener(MouseEvent.MOUSE_DOWN, checkForm);
player.text = "";
}
public function checkForm (event:MouseEvent):void {
if (player.text != ""){
gotoAndStop(1);
sendForm();
}
else{
player.text = "please enter your name";
}
}
Try MovieClip(root).gotoAndStop(1); - assuming you're trying to change frames on the main timeline and not an object.
Also, it's not clear where you're using this code, (on the timeline, in a class, or in the main .as) but stop(); should be placed in the actions panel of every frame of the timeline / movieClip.

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;
}

AS3 - Timer - Error #1009: Cannot access a property or method of a null object reference

I have created a simple math game for preschool using flash cs5.5. My problem is :
When I click Start to play a game for first time, everything is okay but when I click button option or help, then back to main screen to play again, my timer and my score are hidden but it's running. How do I solve that?
Here is my code for the timer :
var count:Number = 5; // amount of time
var myTimer:Timer = new Timer(1000,count);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent):void {
timer_txt.text = String((count)-myTimer.currentCount); //dynamic txt box shows current count
if (((count)-myTimer.currentCount) == 0) {
gotoAndStop(1, "Scene 8");
}
}
And the error :
Error #1009: Cannot access a property or method of a null object reference.at FWM_MENU_fla::MainTimeline/countdown()[FWM_MENU_fla.MainTimeline::frame51:37]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
Before going to Help:
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, countdown);
If you wait until after, myTimer will be out of scope and probably permanently inaccessible. The 1009 is probably timer_txt, which likely doesn't exist wherever you are when the timer fires (likely in the Help).

'Error #1009: Cannot access a property or method of a null object reference.' in Flash CS5?

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at src::Game/onClick()
This is the full error^, however it doesn't give me the error on the timeline, but the output, when the flash game is played. Here's the code: http://pastebin.com/FnjWCQJ8 , the error's in line 35 or 49.
Thank you.
Its because blue1,blue2,and blue3 are not defined anywhere.
private function onClick(m:MouseEvent):void{
blue1.startme = true;
blue2.startme = true;
blue3.startme = true;
}
Probably line 31 - your stage instance in the Game constructor. Your MovieClip is probably not on the stage yet. Use an ADDED_TO_STAGE event listener, then add listeners to stage on added to stage handlers.
/*
* Constructor.
*/
public function Game()
{
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
protected function addedToStageHandler(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
// Listeners.
stage.addEventListener(Event.ENTER_FRAME, _update);
stage.addEventListener(MouseEvent.MOUSE_DOWN, _mouseAction);
stage.addEventListener(MouseEvent.MOUSE_UP, _mouseAction);
stage.addEventListener(MouseEvent.CLICK, onClick);
// Helicopter.
_helicopter = new Helicopter();
stage.addChild(_helicopter);
}

AS3 - Error #1009: Cannot access a property or method of a null object reference

I'm making a game where if the enemy's bullet hits the user, the bullet disappears. Everything works fine except that I keep getting,
'Error #1009: Cannot access a property or method of a null object
reference'
Once the bullet hits the user (bullet disappears though). It confuses me because I've used almost exact the same code in another class where it works perfect.
package classes.enemy
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
import classes.Main;
public class Bullet extends MovieClip
{
var speed:Number;
public function initialize()
{
var stageReff:Stage = this.stage as Stage;
stage.addEventListener("enterFrame", enterFrame);
}
//code
function enterFrame(e:Event):void
{
this.x += speed;
if(this.hitTestObject(Main.user))
{
removeEventListener("enterFrame", enterFrame);
this.parent.removeChild(this);
// line above gives the error.
}
}
}
}
I have no clue what could be wrong.
Thanks in advance.
My guess is it has to do with the fact that you're adding an enter frame event listener to the stage, yet try to remove it from the listening object itself.
Try changing
var stageReff:Stage = this.stage as Stage;
stage.addEventListener("enterFrame", enterFrame);
to
addEventListener("enterFrame", enterFrame);
you're not removing the event listener from the stage, but the object itself :)
I would not recommend doing it like this, create one listener in your main game class, and call an update function on all objects!
The reason why it fails is because you're removing your Bullet from it's parent. So when you're referencing this.parent it returns null because there simply isn't a parent anymore. You're trying to remove the ENTER_FRAME event but because you set it on the stage the original ENTER_FRAME event keeps running. You can simply fix it like so:
if(this.x > 30)
{
stage.removeEventListener("enterFrame", enterFrame);
this.parent.removeChild(this);
}
But like #RasmusWriedtLarsen already pointed out, it's better to handle these events more globally. And also let the parent handle the removing of the Bullet.