Reloading same cocos2d scene shows pink screen - uiviewcontroller

I am making a game in cocos2d. Game Scene has a menu button to take back to main menu which is a UIViewController. When user chooses to play again and same game scene is called to run in director, a pink screen appears on top of game scene.
It could be because of replacing the same scene with itself. So, I replaced it with an empty scene first when main menu is called. Code replacing scene is:
if ([[CCDirector sharedDirector] runningScene] == NULL) {
[[CCDirector sharedDirector] runWithScene: [MySceneLayer scene]];
}
else {
[[CCDirector sharedDirector] replaceScene:[MySceneLayer scene]];
}
It checks, if there is no scene running then starts the game scene first time. If some scene is already running, which in this case is an empty scene, then replaces it with game scene. The dealloc of game scene is also called which means that old scene is destroyed properly. Even then, replacing empty scene with game scene gives pink screen while replacing with any other new scene does not give any problem.
What can be the reason and what is the solution?

Finally, the problem caught and resolved. All it needs is careful play of adding and removing views. I was removing OpenGLView from superview when coming back to menu in UIViewController. While replacing the scene again, same
[[CCDirector sharedDirector] openGLView]
which was in use earlier, had to be added back to the application window. Right place and right timing did it.
Any minor error in views or sprites can show you with pink screen :)

Related

How to make a movieclip play on a specific frame in AS3?

I'm incredibly rusty at Flash having not touched it in probably 10 years and can't seem to figure this out, or find it online:
I have a MovieClip with two layers, each having a Shape Tween. Basically its a Door that opens and closes.
I dropped it onto the main timeline but now I need it to start and stop. This is where I'm now struggling since the last time I used Flash actions could go on specific keyframes.
I made a new layer called actions just to keep things organized and currently have:
barrier1.stop();
I just want something that lets me state a frame, say 57 to have barrier1 start playing on. Tried using play(); and Event.ENTER_FRAME with no luck. How would I set this up?
Well it is easy with the instance name of your movieClip
barrier1.stop(); // Stops the movieClip
barrier1.play(); // Resumes
barrier1.gotoAndStop(12) // Goes to 12nd frame and stop
barrier1.gotoAndPlay(12) // Goes to 12nd frame and play
barrier1.currentFrame // returns barrier currentframe
For capturing frame from scene level:
this.addEventListener(Event.ENTER_FRAME,onLoop);
function onLoop(event:Event){
if(barrier1.currentFrame == 57){
trace("BARRIER is in 57. frame");
}
}
Inside on the animation clip on the first frame
var root:MovieClip = this.parent as MovieClip
root.makeStartSceneAnimation()
**in timeline scene level [root]**
function makeStartSceneAnimation(){
/// barrier started to play
}
If you are using timeline, you can add Key frame on the desired frame, and then add stop(); as Action in the action layer. But bear in mind that if you do this in the main timeline - it will stop everything. If you want to stop that MovieClip, then you have to do this inside MoviceClip's timeline.

Movie Clips frozen but swf has not crashed - AS3 CS6

I'm making a top down shooter for a college project. I'm getting extremely low on available time left to complete this.
I followed a tutorial as we was not taught any AS3 although expected to creating a game in this language so there is commented code due to this. The tutorial lead me up to creating a player on the stage that moves by keyboard presses and can fire bullets by pressing the left mouse button, but it didn't help with creating enemies at all.
So I created an enemy movie clip that just spawns on the stage at X=0 and Y=0 (random spawning will be added later), and it's supposed to move towards the players location but it doesn't. When the swf loads up it appears they have started to move but have frozen on the screen. It's not crashed because I can still move the player around and fire bullets? Any help is appreciated.
Main.as code
Enemy.as code
Well, you are not calling the moveTowards() in any loop -> the position is not updated. Or where exactly are you updating the enemy's position? I see only one setting of .x, .y of enemy and it is right after you create the object, no further updates.
Put this into your loop function in Main.as:
for(var i:int=0; i<enemies.length; i++) {
Enemy(enemies[i]).moveTowards(player.x, player.y);
}

how to set actionscript code to execute only once

Okay so I have a movieclip called a_mc, if you click the movieclip, it goes to frame 5, and then on frame 5 there is a button called close_btn where if you click the button, it goes back to frame 1 and it is supposed to make a_mc invisible. Here is the actionscript code for frame 1.
stop();
a_mc.addEventListener(MouseClick.CLICK, aClicked);
function aClicked(event:MouseEvent):void {
gotoAndStop(5);
}
and on frame 5, the actionscript code is
stop();
close_btn.addEventListener(MouseEvent.CLICK, closeCLicked);
function closeClicked(event:MouseEvent):void {
gotoAndStop(1);
a_mc.visible = false;
a_mc.removeEventListener(MouseEvent.CLICK, aClicked);
}
see, the problem is, in frame 5, I make a_mc invisible and remove the event listener and go back to frame 1 and on frame one, it always executes the actionscript code so it again creates the event listener and makes a_mc visible. Any idea on how to stop this from happening?
I tried putting the code from frame 1 into a package and then a class and then a constructer method but it is saying
"Syntax error: package is unexpected"
Could you put all the code that you want to execute once in frame 1? - don't call stop() and let it run to the next frame.
Then put the rest of your code in other key frames and don't use gotoAndStop(1) so frame 1 is only called once?
You can try not removing the event listener on a_mc in frame 5, and then in frame 1 check if the event listener is already present (a_mc.hasEventListener()) as a signal that frame 1 has already been shown. Not exactly a 'bets practices' solution, but it might work.
Unfortunately, depending on the actual conents of those clip, and what happens in other frames, it may be the problem you're having is a consequence of the way movieclip object works in flash. When a frame is changed, flash instantiates new objects on the stage (added in new frame), and removes the ones not needed anymore (depending on the contents, but generally it's true). The 'a_mc' object that you manipulate in frame 5 may not be the same 'a_mc' object that is on the stage when you go back to frame 1. It may have been deleted and reinstantiated in the meantime.
To avoid things like that, it would be a better solution to have controlling code in a class outside of the timeline of the animating clip, or at least to keep the state in a separate object. I work in Flash Builder so I can't help you with the details of such organization in Flash Pro (which I presume you're using), but you could probably have all code on the frame 1 of the main clip, and then put the other movieclips with buttons and stuff as children of the main clip. That way main clip can control the state, and know what to show when.

Clip staying on screen while not in timeline anymore

I have a particular problem : I have a fla with 2 frames, with a clip of instance name "my_clip" on frame 1 and no clip en frame 2. If i move by code (like a tween) "my_clip", when I go to frame 2, the clip is still on screen, whereas frame 2 is totally empty in my fla.
How flash work at this level ? Does he stop "syncronizing" a clip with the fla once the clip has been moved by code ? How can I check at frame 2 that the scene should be empty ?
(I'm with flash cs 5.5 and player 11)
Actually what FLASH does when we are trying to change the depth or start drag and some more dynamic functionality it freezes the target movieclip in stage. Eventually there is only one stage in FLASH and guess what if the movieclip has been frozen in the stage? It is visible or available always.
So in your case the same has happened. The clip has just frozen to the stage. Needless to say, the second frame is empty always. You don't need to check the second frame. What all you need to do is remove that clip before you navigates to either next frame or scene.
#LondonDrugs_MediaServices : I can't post my code and fla because there is too much code unrelated to the problem.
But as I was trying to make a little fla to reproduce my problem and show it to you, I figure out that the problem was not where I thought it was : I fact it was because each time I start dragging my clip i was doing a setChildIndex on it to put it on top. And it was precisely the setChildIndex wich would cut the "syncronization" between the clip and the fla timeline.
Thanks anyway for your answers, it kind of help me in a way :)

how do I stop a scene and play the next scene in adobe flash cs6 actionscript 3.0

I'm trying to make a loop animation scene where its like an eye opening and closing. I have one black rectangle that goes down and meets with another black rectangle going up to create the blink effect. Then in the middle I have a button that the viewer can click to go to the next scene. The problem is that before and after the button is pressed both scenes play simultaneously. How can I play the first loop scene and then stop it to play the next scene when a user presses that button? Thanks.
You need to tell your respective scenes to stop. Otherwise flash player will just move on to the next scene automatically after the last frame of the previous scene (and loop back to the first scene after the last frame of the final scene).
In flash, open your first scene and open the timeline view. scroll to the very end of your timeline, add a new layer, on the very last frame of your timeline on your new layer, create a keyframe (F6 on windows). With that new keyframe selected, open the code editor (F9 on windows) and put in the command stop(); OR if you want the current scene to keep looping (without moving on the next scene) put in the command gotoAndPlay(1);
If you want your other scene to stop as well at the end, repeat the above steps on it's timeline.