• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Need help when swapping rooms in my point and click game

New Here ,
Jun 21, 2012 Jun 21, 2012

Copy link to clipboard

Copied

Ok i am making my first point and click game in flash to familiarize myself with some of the basics.

I have set up a room with a 2 walls(rooms) at the moment, and on the first frame there are 4 screws that when clicked play a small animation and are then removed, works fine so far.  But if i leave that room then go back into it the screws are back in their original position.  How do i stop this from happening, i am guessing i have to put an if formula somewhere on my addChild(TLscrew) or put it somewhere else besides in the enter_frame function.  Anyway heres my code any help would be appreciated. 

import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;

stop();

myWords.addEventListener(MouseEvent.CLICK, cleartext);
doorHandle.addEventListener(MouseEvent.CLICK, losehandle);
doorBit.addEventListener(MouseEvent.CLICK, turnDoorBit);
tele.addEventListener(MouseEvent.CLICK, turnOnTele);
skyremote.addEventListener(MouseEvent.CLICK, gotoRemoteBack);


addEventListener(Event.ENTER_FRAME, loop);

var TLScrew:MovieClip = new screw();
var TRScrew:MovieClip = new screw();
var BLScrew:MovieClip = new screw();
var BRScrew:MovieClip = new screw();
var topBattery:MovieClip = new AABattery();
var bottomBattery:MovieClip = new AABattery();
var remoteHand:MovieClip = new Hand();
var TLout:Boolean = false;
var TRout:Boolean = false;
var BLout:Boolean = false;
var BRout:Boolean = false;
var doorBitStraight:Boolean = true;
var teleSwitchedOn:Boolean = false;
var beenToOtherRoom:Boolean = false;

TLScrew.addEventListener(MouseEvent.CLICK, screwout1);
TRScrew.addEventListener(MouseEvent.CLICK, screwout2);
BLScrew.addEventListener(MouseEvent.CLICK, screwout3);
BRScrew.addEventListener(MouseEvent.CLICK, screwout4);


function loop(e:Event):void{

tele.buttonMode = true;
TLScrew.buttonMode = true;
TRScrew.buttonMode = true;
BLScrew.buttonMode = true;
BRScrew.buttonMode = true;
doorHandle.buttonMode = true;

addChild(TLScrew);
  TLScrew.x = 258;
  TLScrew.y = 205.1;
 
addChild(TRScrew);
  TRScrew.x = 268;
  TRScrew.y = 205.1;

addChild(BRScrew);
  BRScrew.x = 268;
  BRScrew.y = 230.35
 
addChild(BLScrew);
  BLScrew.x = 258;
  BLScrew.y = 230.35;



if(TLScrew.currentFrame == 24){
  removeChild(TLScrew);
}
if(TRScrew.currentFrame == 24){
  removeChild(TRScrew);
}
if(BLScrew.currentFrame == 24){
  removeChild(BLScrew);
}
if(BRScrew.currentFrame == 24){
  removeChild(BRScrew);
}

}
function losehandle(e:MouseEvent):void{
myWords.text = "handle fell off";
doorHandle.gotoAndPlay(2);
doorHandle.removeEventListener(MouseEvent.CLICK, losehandle);
}

function turnDoorBit(e:MouseEvent):void{
if(TLout == true && TRout == true && BLout == true && BRout == true && doorBitStraight == true){
  doorBit.rotation = 75;
  doorBitStraight = false;
}
else if(TLout == true && TRout == true && BLout == true && BRout == true && doorBitStraight == false){
  doorBit.rotation = 360;
  doorBitStraight = true;
}
}

function gotoRemoteBack(e:MouseEvent):void{
gotoAndStop(2);
addChild(topBattery);
addChild(bottomBattery);
addChild(remoteHand);
topBattery.x = 174.8;
topBattery.y = 134.75;
bottomBattery.x = 127.8;
bottomBattery.y = 192.8;
bottomBattery.rotation = 180;
remoteHand.x = 360.3;
remoteHand.y = 143.75;
arrowBack.addEventListener(MouseEvent.CLICK, gotoMain);
beenToOtherRoom = true;
}

function gotoMain(e:MouseEvent):void{
removeChild(topBattery);
removeChild(bottomBattery);
removeChild(remoteHand);
gotoAndStop(1);


}


function turnOnTele(e:MouseEvent):void{
if(teleSwitchedOn == false){
tele.gotoAndStop(2);
teleSwitchedOn = true;
}else if(teleSwitchedOn == true){
  tele.gotoAndStop(1);
  teleSwitchedOn = false;
}
}

function cleartext(e:MouseEvent):void{
myWords.text = "";
}


function screwout1(e:MouseEvent):void{
TLScrew.gotoAndPlay(2);
TLout = true;

}

function screwout2(e:MouseEvent):void{
TRScrew.gotoAndPlay(2);
TRout = true;
}

function screwout3(e:MouseEvent):void{
BLScrew.gotoAndPlay(2);
BLout = true;
}

function screwout4(e:MouseEvent):void{
BRScrew.gotoAndPlay(2);
BRout = true;
}


TOPICS
ActionScript

Views

1.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jun 21, 2012 Jun 21, 2012

Instead of having that code to remove them, you could try letting them remove themselves.  In the last frame (24) within each screw mc, try using...

MovieClip(parent).removeChild(this);

Votes

Translate

Translate
LEGEND ,
Jun 21, 2012 Jun 21, 2012

Copy link to clipboard

Copied

If the intention is to not have the screws appear when you return, then one way to deal with that is to isolate all of the code that creates the screws into functions and conditionals and have a boolean variable, maybe named "screwsUsed" or something like that, that you declare without assigning a value...

var screwsUsed:Boolean;

and then in a function where you want to decide if you create the screws or not you use that boolean .  You do not assign it a value (true) until after the screws have been used the first time.

if(!screwsUsed){   // note the " ! " meaning NOT

   // use the screws

   // sometime later, not necessarily in this section

   screwsUsed = true;

}

What happens is the screwsUsed variable falls out as false because it is null (no value) at the start.  When you assign it a true value, it sticks because when you return to that frame, the value is not being assigned.

Like the screwsUsed variable, you can declare the screws outside any functions so they are available to any function, and only instantiate them inside that conditional. This way you only create instances when you need them, not each time you enter that frame...

var TLScrew:screw;

var TRScrew:screw;

var BLScrew:screw;

var BRScrew:screw;

if(!screwsUsed){   // note the " ! " meaning NOT

   // use the screws  

   TLScrew = new screw();

   TRScrew = new screw();

   BLScrew = new screw();

   BRScrew = new screw();

   // sometime later, not necessarily in this section

   screwsUsed = true;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 21, 2012 Jun 21, 2012

Copy link to clipboard

Copied

Hi i have looked over your reply but still i am having no luck getting this to work.  If you look at my code, the BRout, BLout, TLout, TRout are the Boolean' i am using instead of your "screwsUsed" this is because there are 4 individual screws and if someone removes just one then leaves the frame i want it so that when they come back only that one is missing.  Can i just let you know that i am not using classes for this.  I tried the following:

if(!BLout){

addChild(TLScrew);

  TLScrew.x = 258;

  TLScrew.y = 205.1;

}

this is inside my ENTER_FRAME function but i am still having no luck.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 21, 2012 Jun 21, 2012

Copy link to clipboard

Copied

If you are using the BLout, etc, then like I said with the screwsUsed, you should only declare it, do not assign it a false value or else when you return to that frame the false value gets reassigned.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 21, 2012 Jun 21, 2012

Copy link to clipboard

Copied

Here is my new code including the suggestion of taking the false out of my Booleans which has got me a step closer to where i want to be (thanks) but now with this code when i click my TLscrew it plays the movieclip then when it reaches the end of the animation it removes itself(as intended), but none of the other screws are removing themselves when they reach the frame 24 like they are meant to.  Any ideas where i have went wrong?

Thanks for the help

import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;

stop();


var topBattery:MovieClip = new AABattery();
var bottomBattery:MovieClip = new AABattery();
var remoteHand:MovieClip = new Hand();
var TLout:Boolean;
var TRout:Boolean;
var BLout:Boolean;
var BRout:Boolean;
var doorBitStraight:Boolean = true;
var teleSwitchedOn:Boolean = false;
var TLScrew:MovieClip = new screw();
var TRScrew:MovieClip = new screw();
var BLScrew:MovieClip = new screw();
var BRScrew:MovieClip = new screw();

myWords.addEventListener(MouseEvent.CLICK, cleartext);
doorHandle.addEventListener(MouseEvent.CLICK, losehandle);
doorBit.addEventListener(MouseEvent.CLICK, turnDoorBit);
tele.addEventListener(MouseEvent.CLICK, turnOnTele);
skyremote.addEventListener(MouseEvent.CLICK, gotoRemoteBack);
addEventListener(Event.ENTER_FRAME, loop);


TLScrew.addEventListener(MouseEvent.CLICK, screwout1);
TRScrew.addEventListener(MouseEvent.CLICK, screwout2);
BLScrew.addEventListener(MouseEvent.CLICK, screwout3);
BRScrew.addEventListener(MouseEvent.CLICK, screwout4);


function loop(e:Event):void{

tele.buttonMode = true;
TLScrew.buttonMode = true;
TRScrew.buttonMode = true;
BLScrew.buttonMode = true;
BRScrew.buttonMode = true;
doorHandle.buttonMode = true;


if(!TLout){
addChild(TLScrew);
  TLScrew.x = 258;
  TLScrew.y = 205.1;
}

if(!TRout){
addChild(TRScrew);
  TRScrew.x = 268;
  TRScrew.y = 205.1;
}

if(!BRout){
addChild(BRScrew);
  BRScrew.x = 268;
  BRScrew.y = 230.35
}

if(!BLout){
addChild(BLScrew);
  BLScrew.x = 258;
  BLScrew.y = 230.35;
}


if(TLScrew.currentFrame == 24){
  removeChild(TLScrew);
}
if(TRScrew.currentFrame == 24){
  removeChild(TRScrew);
}
if(BLScrew.currentFrame == 24){
  removeChild(BLScrew);
}
if(BRScrew.currentFrame == 24){
  removeChild(BRScrew);
}

}
function losehandle(e:MouseEvent):void{
myWords.text = "handle fell off";
doorHandle.gotoAndPlay(2);
doorHandle.removeEventListener(MouseEvent.CLICK, losehandle);
}

function turnDoorBit(e:MouseEvent):void{
if(TLout == true && TRout == true && BLout == true && BRout == true && doorBitStraight == true){
  doorBit.rotation = 75;
  doorBitStraight = false;
}
else if(TLout == true && TRout == true && BLout == true && BRout == true && doorBitStraight == false){
  doorBit.rotation = 360;
  doorBitStraight = true;
}
}

function gotoRemoteBack(e:MouseEvent):void{
gotoAndStop(2);
addChild(topBattery);
addChild(bottomBattery);
addChild(remoteHand);
topBattery.x = 174.8;
topBattery.y = 134.75;
bottomBattery.x = 127.8;
bottomBattery.y = 192.8;
bottomBattery.rotation = 180;
remoteHand.x = 360.3;
remoteHand.y = 143.75;
arrowBack.addEventListener(MouseEvent.CLICK, gotoMain);
beenToOtherRoom = true;
}

function gotoMain(e:MouseEvent):void{
removeChild(topBattery);
removeChild(bottomBattery);
removeChild(remoteHand);
gotoAndStop(1);
 
}


function turnOnTele(e:MouseEvent):void{
if(teleSwitchedOn == false){
tele.gotoAndStop(2);
teleSwitchedOn = true;
}else if(teleSwitchedOn == true){
  tele.gotoAndStop(1);
  teleSwitchedOn = false;
}
}

function cleartext(e:MouseEvent):void{
myWords.text = "";
}


function screwout1(e:MouseEvent):void{
TLScrew.gotoAndPlay(2);
TLout = true;
TLScrew.removeEventListener(MouseEvent.CLICK, screwout1);
}

function screwout2(e:MouseEvent):void{
TRScrew.gotoAndPlay(2);
TRout = true;
}

function screwout3(e:MouseEvent):void{
BLScrew.gotoAndPlay(2);
BLout = true;
}

function screwout4(e:MouseEvent):void{
BRScrew.gotoAndPlay(2);
BRout = true;
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 21, 2012 Jun 21, 2012

Copy link to clipboard

Copied

Instead of having that code to remove them, you could try letting them remove themselves.  In the last frame (24) within each screw mc, try using...

MovieClip(parent).removeChild(this);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 22, 2012 Jun 22, 2012

Copy link to clipboard

Copied

LATEST

Hi this seems to have done the job but my code must be so messed up because i now have another problem.  If i remove 2 screws leave the frame and come back they are missing which is what i want, but if i then click the other 2 screws to remove them after i have left the frame already they do not remove, they still show the animation but do not remove themselves after it, any ideas? thanks again for the mulitple replies

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines