PDA

View Full Version : Refreshing the screen?



JapanMan
September 3rd, 2008, 05:55 PM
I'm working on a game, and I have all the actual game-play dynamics figured-out and working, but if you lose and start over, it imprints all the falling enemies on the screen for a minute or so. How can I stop this?

The Game
September 3rd, 2008, 06:14 PM
You'll need to give more information than that, there are many possible reasons depending on how you're build your game so far.

Please explain how you are getting the enemies on to the screen in the first place; that is important to know if you want help removing them.

Also, is it AS2 or AS3?

JapanMan
September 4th, 2008, 03:45 PM
I'm using Flash MX.
The game is organized all in one Scene. I have the first few frames set aside for the main menu (labeled "menu"), and the next few for the "how to play" screen (labeled "help"). When you push the Play button from either the help screen or main menu, it takes you to the game, which has the following code:

(on a frame before it adds the player or enemies)


score = 0;
life = 3;
(on the next frame, the actual game)


var bulletSpeed = 15;
var bulletReady = true;
var bulletDelay = 500;
var bulletArray = [];
var bulletCount = 0;
function createBullets() {
var bulletMc = this.attachMovie("bullet", "bullet"+bulletCount, 1000+bulletCount);
bulletCount++;
bulletMc._x = random(610)+20;
bulletMc._y = -75;
bulletArray.push(bulletMc);
}
function moveBullets() {
if (bulletReady) {
trace(bulletArray.length);
bulletReady = false;
currentTime = getTimer();
createBullets();
} else {
if (currentTime+bulletDelay<=getTimer()) {
bulletReady = true;
}
}
for (var i = 0; i<bulletArray.length; i++) {
var b = bulletArray[i];
if (b._y<750) {
bulletArray[i]._y += bulletSpeed;
} else {
removeMovieClip(b);
bulletArray.splice(i, l);
}
if (!hit) {
if (b.hitTest(ShipMc)) {
ShipMc.play();
bulletArray.splice(i, l);
}
}
}
}
var hit = false;
function createObject(o) {
ob = this.attachMovie(o, o, 100);
ob._x = random(600)+20;
ob._y = random(480)+20;
}
function resetObject() {
ob._x = random(600)+20;
ob._y = random(480)+20;
ob.restart = false;
hit = false;
}
createObject("rock");
this.onEnterFrame = function() {
moveShip();
moveObject();
moveBullets();
};
var speed = 8;
var shipMc = this.attachMovie("ship", "ship", 10000);
shipMc._y = Stage.height-(shipMc._height+10);
shipMc._x = Stage.width/2-(shipMc._width/2);
function moveShip() {
if (Key.isDown(Key.UP)) {
shipMc._y -= speed;
}
if (Key.isDown(Key.DOWN)) {
shipMc._y += speed;
}
if (Key.isDown(Key.LEFT)) {
shipMc._x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
shipMc._x += speed;
}
if (!hit) {
if (shipMc.hitTest(ob)) {
hit = true;
resetObject();
score++;
}
}
}
stop();
so, I have no problem removing the enemies (named "bullet") during play, but if you lose, got to the "lose" screen, and return to the menu, not only are the the spikes still falling, but the ones that are falling when you press Play the second time get stuck on the screen.

I can attack the .swf if you want.

Smee
September 10th, 2008, 06:25 PM
You can create empty MovieClips (containers) for the separate things like the bullets and the spikes, and then attach them into their container, like:


createEmptyMovieClip("spikeContainer", 0);
createEmptyMovieClip("bulletContainer", 1);

bulletContainer.attachMovie("bullet", "bullet" + bulletCount, bulletCount++);
spikeContainer.attachMovie("spike", "spike" + spikeCount, spikeCount++);

Then when you want to remove the spikes, just remove the container.

Using this, you can use better depth management; you just need to order the depths of the containers, and each container has it's own set of depths that it can devote to the type of object it contains.

JapanMan
September 11th, 2008, 11:44 AM
I don't think that's the problem, though it is a interesting way of removing the array parts. I'm looking for something more like "stopfunction()". That's the other problem, is the reason they are even on the screen when it starts is because it doesn't stop creating spikes when you lose, even though there's only one frame with the code for that in it.

Would updateAfterEvent() work?

Smee
September 11th, 2008, 12:48 PM
If it's creating spikes using onEnterFrame or setInterval, you need to clear that because they'll keep going even when you change frames.

JapanMan
September 11th, 2008, 03:23 PM
that's exactly how its being done. How do I stop it?

Smee
September 11th, 2008, 03:30 PM
If you're using onEnterFrame, you can use:


delete onEnterFrame;

If you're using setInterval, you can use:


clearInterval(intervalID);

The 'intervalID' is the integer returned from the setInterval method. So, to create and clear two intervals:


intervalID1 = setInterval(Function, 0);
intervalID2 = setInterval(Function, 0);

trace(intervalID1); // Output: 1
trace(intervalID2); // Output: 2

// Two ways of clearing the intervals
// Clears the first interval
clearInterval(intervalID1);

// Clears the second interval
clearInterval(2);

JapanMan
September 11th, 2008, 03:50 PM
stopping the creating works fine, but they still just freeze on the screen for a minute. What I need to is to clear the stage too.

btw, man - and everyone who's been helping - thanks a ton; I know I'm just asking for one thing after another. Means a lot.

Smee
September 11th, 2008, 06:45 PM
Well, breaking off the code that creates them and removing them from the screen, by either looping through an array that contains them or removing the container that they were attached to, should do it. If both things don't work, then you'll need to provide more too look at, as it seems to be an unusual case.