Copy link to clipboard
Copied
this is an rpg game where my character walks around the field and a battle scene randomly comes up and you have to fight the monster.
when the battle scene comes up, my character stops moving and it enters the battle scene. When the battle is finished and i press an okBtn to return to the field, my character is expected to start moving again which worked for a while. but now it doesnt. There is an endTheBattle variable which is false the whole game time.
When i press the okBtn, it makes it endTheBattle true for one split second and then becomes false. When the endTheBattle is true, it re adds the enter frame evnet listener which should make the character move again. However it doesnt work. I trace the variable in the main class and i see the 'true' come up. But when i trace it in my character class which holds the enter frame event listener, it doesnt come up.
Copy link to clipboard
Copied
first, you need to make sure the class that needs to 'see' endTheBattle, actually 'sees' it. then you need to make sure there's just one endTheBattle or, if there's more than one, that they update each other. use the trace function to confirm.
if endTheBattle is true momentarily and then false, there's probably a loop (eg, enterframe) that's resetting that variable's value. find all the places that could do that and use trace to pinpoint the problem.
Copy link to clipboard
Copied
Yes I want to make the variable true for one split second ( which happens when I end the battle scene by pressing the okBtn) so it adds the enter frame event listener once. When it goes back to the field, it restates it as false.
the main class controls the variable
In my character class there is an enter frame event called "backup" that says
if(endTheBattle == true)
addEventlistener(enterframe, move character);
in this "backup" enter frame event, I traced endTheBattle but the true statement never comes up. I make a separate enterframe function in the main class and I trace endTheBattLe and it shows up for that split second
Copy link to clipboard
Copied
you don't have an enterframe event called 'backup'.
you might have an enterframe listener function called backup, if your code looks like;
something.addEventListener(Event.ENTER_FRAME,backup);
function backup(e:Event):void{
.
.
if(endTheBattle){
etc
.
.
}
}
is that what you mean?
if so, in that same class that has the function backup() you are able to trace endTheBattle and it shows true (momentarily), correct?
if so, in backup(), endTheBattle never traces true, correct?
if so, use some text so you can determine which variable your tracing so you can ensure there's a moment when backup is called and endTheBattle is true in our other location.
you might find, your timing is off or, more likely, you'll find you have two different endTheBattle variables. (eg, one may be local to a function).
Copy link to clipboard
Copied
sry i wasnt being too clear
character class:
public function PixelCharacter()
{
addEventListener(Event.ENTER_FRAME, backUp);
addEventListener(Event.ENTER_FRAME, moveCharacter);
}
public function backUp(event:Event)
{
trace(MovieClip(parent).endTheBattle); //trace here doesnt trace the 'true'
if(MovieClip(parent).endTheBattle == true)
{
addEventListener(Event.ENTER_FRAME, moveCharacter);
}
}
main class:
public function PixelGame()
{
endTheBattle = false
MainScreen();
addEventListener(Event.ENTER_FRAME, something);
}
public function something(event:Event)
{
//trace(endTheBattle); //this one traces the 'true'
}
somewhere in the main class:
victoryScreen.okBtn.addEventListener(MouseEvent.CLICK, endBattle);
public function endBattle(event:MouseEvent)
{
blah blah removing battle scene objects
endTheBattle = true; //for adding back the enterFrame event listener moveCharacter
addEventListener(Event.ENTER_FRAME, firstGrassloop); //this is the loop of the field where you walk around. in this function it contains endTheBattle = false, so it prevents the backup function from adding the moveCharacter multiple times in a loop. This function is removed when the battle scene comes up.
Copy link to clipboard
Copied
1. MovieClip(parent).whatever is bad coding.
2. is PixelCharacter a child of PixelGame?,
3. is endTheBattle a private variable in PixelGame?
Copy link to clipboard
Copied
1. So what do i use to access variables from the main class?
2. PixelCharacter is a child of PixelGame
3. endTheBattle is a public variable in PixelGame
when i set endTheBattle = true in some other enter frame function in PixelGame, this if statement works.
In the endBattle function, it declares it as true for a split second but it seems like PixelGame is only able to see that 'true'
being declared and not PixelCharacter
public function backUp(event:Event)
{
trace(MovieClip(parent).endTheBattle); //trace here doesnt trace the 'true'
if(MovieClip(parent).endTheBattle == true)
{
addEventListener(Event.ENTER_FRAME, moveCharacter);
}
}
Copy link to clipboard
Copied
1. you should make the variable private and allow access via a public getter.
do you see a trace from backUp() after endTheBattle is true in PixelGame and before endTheBattle is false in PixelGame?
Copy link to clipboard
Copied
i cant really tell, if i trace it in pixelCharacter all i get else false false false...etc.
when i trace it in PixelGame, i get false false false false TRUE false false false...etc
Copy link to clipboard
Copied
again, add something to one or both so you tell which is which. eg, in backUp():
trace(this,MovieClip(parent).endTheBattle);
Copy link to clipboard
Copied
this,MovieClip(parent) doesnt seem to work.
and what do you mean by the which is which?
which is supposed to be what?
Copy link to clipboard
Copied
OH nevermind i get you im just a beginner but heres the outputs
[object PixelCharacter] false
false
[object PixelCharacter] false
true
[object PixelCharacter] false
false
[object PixelCharacter] false
false
pretty weird
Copy link to clipboard
Copied
show your declaration of endTheBattle in PixelGame
show the function where you have your trace (in PixelGame)
show where you add PixelCharacter instance to PixelGame
Copy link to clipboard
Copied
public class PixelGame
public var endTheBattle:Boolean = false
public var hero:PixelCharacter;
public function PixelGame()
{
hero = new PixelCharacter;
addChild(hero);
addEventListener(Event.ENTER_FRAME, something);
addEventListener(Event.ENTER_FRAME, firstGrassLoop);
}
public function something(event:Event)
{
trace(endTheBattle);
}
public function firstGrassArea(event:Event)
{
endTheBattle = true;
if(RRNG == 1)
{
redSlimebattle = true;
battle = true;
}
if(redSlimebattle == true)
{
addEventListener(Event.ENTER_FRAME, redSlimeBattleloop);
removeEventListener(Event.ENTER_FRAME, firstGrassloop);
}
}
public function redSlimeBattleLoop(event:Event)
{
if((redSlime.health <= 0)&&(victory == false))
{
victory = true;
victoryScreen.okBtn.addEventListener(MouseEvent.CLICK, endBattle);
}
public function endBattle(event:MouseEvent)
{
blah blah removing battle scene objects
endTheBattle = true;
addEventListener(Event.ENTER_FRAME, firstGrassloop);
}
this should be everything for the main class
everything was working fine before. The last actual change i made in the game was i added a custom cursor and the next day this stopped working so idk.
Copy link to clipboard
Copied
that doesn't look right. PixelGame's not extending a displayobjectcontainer (eg, a movieclip).
and you should have
hero = new PixelCharacter();
put something in that PixelGame class so you can confirm you're really using it, eg
public function something(event:Event)
{
trace(this,endTheBattle);
}
Copy link to clipboard
Copied
Oh sry forgot to put that in the reply, I didn't copy and paste cuz I had a lot of code so I typed everything yea so I put
public class PixelGame extends MovieClip
Copy link to clipboard
Copied
you don't have to (and shouldn't) copy and paste everything, but you should copy and paste the relevant snippets. otherwise, you're prone to add errors and remove problems inadvertently.
fix the problems that have already been found (hero and use a getter).
Copy link to clipboard
Copied
i kind of fixed it, but i still don't get why.
public function endBattle(event:MouseEvent)
{
blah blah removing battle scene objects
endTheBattle = true;
addEventListener(Event.ENTER_FRAME, firstGrassloop);
}
the firstGrassloop keeps endTheBattle = false, but when i leave it like that, PixelCharacter doesn't see the true being declared. When i take away endTheBattle = false in the firstGrassloop, PixelCharacter sees the true being declared and it stays true. But thanks for the help.
Copy link to clipboard
Copied
you haven't posted enough code to determine anything additional.
for example, i can't determine if you fixed the two problems already identified and i can't see firstGrassloop()
Copy link to clipboard
Copied
hold on im on to something. I just cleared a bunch of resetting of variables and re adding and removing eventListeners from the endBattle function and i left it with endTheBattle = true and some children being removed. it worked but now i have to find out which code prevents pixelCharacter from seing the true being declared.
Copy link to clipboard
Copied
oh wait nevermind it was the addEventListener(Event.ENTER_FRAME, firstGrassLoop); which is the loop that re declares endTheBattle back to false.
Copy link to clipboard
Copied
I put in a second variable to test it and the same results come out.
I put endTheBattle = true in a different function for example when the okBtn appears. PixelCharacter sees that true declared and adds the moveCharacter event Listener. So there is something with the endBattle function.
Copy link to clipboard
Copied
so in the firstGrassLoop
i changed it to endTheBattle = true; which should constantly add the moveCharacter function. When the battle scene showed up i was still able to move my character. The character class was able to 'see' that true statement in that loop but idk why it can't in the endBattle function.
i also made a separate event listener in the character class and that also couldnt 'see' the true statement in the endBattle function.
So the character class cant 'see' the endTheBattle = true in the endBattle function but my main class can.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now