...

View Full Version : Smoke effect totally ruins my game :/



Ludatha
02-17-2009, 10:11 PM
I am working on a game and it is coming along nicely (need to change some graphics around though)

I downloaded a free smoke effect and I cannot get it to work, here is the fla I downloaded:
http://www.freeactionscript.com/2009/01/realistic-bitmap-smoke-trailer/

Here is the code (I have modified the variable names to see if I had any conflicts, but I didn't:()


var particleCounter:Number = 0;
var currentEffect:String = "particle_smoke_clear";

function doTrail(SMcontainer:MovieClip, SMtargetX:Number, SMtargetY:Number, SMtype:String):Void
{
//set a reference for a new particle and attach a movieclip from the library to that reference
var _particle:MovieClip = SMcontainer.attachMovie(SMtype, SMtype + particleCounter, particleCounter);
//set the particle's x & y position based on the target x & y. Offset the particle by a few pixels
_particle._x = SMtargetX + random(4)-8;
_particle._y = SMtargetY + random(4)-8;
//randomly rotate the particle 360 degrees
_particle._rotation = random(360);
//give the particle a random scale, between 50% and 100%
var randomScale = random(50)+50;
_particle._xscale = randomScale;
_particle._yscale = randomScale;
//give each particle its own speed
_particle.SMspeed = random(5)+3;
updateAfterEvent();
//make it move
_particle.onEnterFrame = function ()
{
this._xscale += this.SMspeed;
this._yscale += this.SMspeed;
this._alpha -= this.SMspeed;
//check if particle is invisible, and remove it
if(this._alpha <= 0)
{
delete this.onEnterFrame;
removeMovieClip(this);
}
}
//update particle counter
particleCounter++
//reset counter if it gets too big
if(particleCounter > 200)
{
particleCounter = 0;
}
}


Here is all my code for the game:


function updateRadar()
{
// Set position of player
vcam.minimap_mc.player_radar_mc._x = _root.soldier._x*0.132;
vcam.minimap_mc.player_radar_mc._y = map_mc.player_mc._y*0.10;

// Set position of enemys
}
var particleCounter:Number = 0;
var currentEffect:String = "particle_smoke_clear";

function doTrail(SMcontainer:MovieClip, SMtargetX:Number, SMtargetY:Number, SMtype:String):Void
{
//set a reference for a new particle and attach a movieclip from the library to that reference
var _particle:MovieClip = SMcontainer.attachMovie(SMtype, SMtype + particleCounter, particleCounter);
//set the particle's x & y position based on the target x & y. Offset the particle by a few pixels
_particle._x = SMtargetX + random(4)-8;
_particle._y = SMtargetY + random(4)-8;
//randomly rotate the particle 360 degrees
_particle._rotation = random(360);
//give the particle a random scale, between 50% and 100%
var randomScale = random(50)+50;
_particle._xscale = randomScale;
_particle._yscale = randomScale;
//give each particle its own speed
_particle.SMspeed = random(5)+3;
updateAfterEvent();
//make it move
_particle.onEnterFrame = function ()
{
this._xscale += this.SMspeed;
this._yscale += this.SMspeed;
this._alpha -= this.SMspeed;
//check if particle is invisible, and remove it
if(this._alpha <= 0)
{
delete this.onEnterFrame;
removeMovieClip(this);
}
}
//update particle counter
particleCounter++
//reset counter if it gets too big
if(particleCounter > 200)
{
particleCounter = 0;
}
}


//This class is used for the grass. It is useful to use raster pixels rather than vector
//graphics because hittests can be performed on individual pixels, and craters are easier to create.
import flash.display.BitmapData;
//Class used when manipulating bitmaps
import flash.geom.Rectangle;
//Another class used when manipulating bitmaps
import flash.geom.Point;
//Initialize the total number of objects to 0. This used to identify instances of objects
totalobjects = 0;
//Initialize the number of passed planes to 0
passedplanes = 0;
//When set to 1, planes and bombs appear.
gameon = 1;
//Array storing the names of all of the enemies on the screen
var enemyarray:Array = new Array();
//This function removes an enemy from the screen, as well as from the enemy array
function removeEnemy(enemyname) {
//loop through all of the enemies, removing it from the array if it exists
for (enemysearch=0; enemysearch<enemyarray.length; enemysearch++) {
//Check if the current index matches the enemy we are looking for
if (enemyarray[enemysearch] == enemyname) {
//There's a match, so remove the enemy from the array
enemyarray.splice(enemysearch,1);
//break the loop
break;
}
}
//Remove the actual enemy instance
removeMovieClip(enemyname);
}
//This function removes all of the enemies from the screen and clears the enemy array
function removeAllEnemies() {
//Loop through the entire enemy array, deleting the enemy instance at the current index
for (enemyloop=0; enemyloop<enemyarray.length; enemyloop++) {
//Remove the enemy at the current index
removeMovieClip(enemyarray[enemyloop]);
}
//All enemies have been deleted, so clear the enemy array
enemyarray = [];
}
//Check if the input object collides with any enemy.
//If it does, remove both and create an explosion
function enemyCollision(collider) {
//Looping through all of the enemies in the array
for (enemysearch=0; enemysearch<enemyarray.length; enemysearch++) {
//Check for a collision
if (eval(collider).hitTest(eval(enemyarray[enemysearch]))) {
//there is a collision, so increment the score
score.score++;
//if the current score is greater than the high score, update the high score
if (score.score>highscore.score) {
//each score is stored within a movieclip instance
highscore.score = score.score;
}
//Create an explosion at the position of the enemy
explosion(eval(enemyarray[enemysearch])._x,eval(enemyarray[enemysearch])._y);
//Remove the enemy
removeEnemy(eval(enemyarray[enemysearch]));
//Remove the input object
removeMovieClip(eval(collider));
//break the for loop
break;
}
}
}

//Create a bitmap that holds the data of the grass
var grasspng:BitmapData = BitmapData.loadBitmap('grass');
//Create another bitmap the size of the grass, and make all of the pixels transparent
var grass:BitmapData = new BitmapData(grasspng.width, grasspng.height, true, 0x00000000);
//Create a movieclip that will hold the grass bitmap
this.createEmptyMovieClip('grasscontainer',this.getNextHighestDepth());
//This function refreshes the grass bitmap filling in spaces that were blown up by bombs
function resetGrass() {
//copy the grasspng bitmap over to the grass bitmap
grass.copyPixels(grasspng,new Rectangle(0, 0, grasspng.width, grasspng.height),new Point(0, 0),grasspng,new Point(0, 0),true);
//attach the grass bitmap to the grasscontainer
grasscontainer.attachBitmap(grass,grasscontainer.getNextHighestDepth());
//move the y position
grasscontainer._y = 390;
}
//apply the grass
resetGrass();

//change the order so the grass is behind the UI
_root.vcam.swapDepths(10);

//Attach the soldier movie clip to the stage
this.attachMovie('soldier','soldier',this.getNextHighestDepth());
//set the soldier's x position to the center
soldier._x = Stage.width/2;
//set the soldier's y position to 360
soldier._y = 360;
//intialize the x and y speeds to 0
soldier.xvel = 0;
soldier.yvel = 0;
//when set to 1, the soldier is not on the ground
soldier.airborne = 1;
//code run for the soldier at the start of each frame


soldier.onEnterFrame = function() {
//loop through the list of enemies and check if the soldier is hitting any of them

for (enemysearch=0; enemysearch<enemyarray.length; enemysearch++) {
//if there is a collision, end the game
if (soldier.hitTest(eval(enemyarray[enemysearch]))) {
//clear all enemies from the screen and show the lose game splash
loseGame();
//make an explosion at the position of the soldier
explosion(soldier._x,soldier._y);
}
}
doTrail(_root, soldier._x, soldier._y, currentEffect);
//If the soldier is airborne, increase the y velocity
if (this.airborne) {
//gravity
this.yvel = Math.min(10, this.yvel+.6);
//if the soldier drops below the screen, lose the game
if (this._y>Stage.height+20) {
//soldier has fallen below the screen, player loses
loseGame();
//reset the soldier's position
this._x = Stage.width/2;
this._y = 30;
}
}
//Set the soldier's x position according to its velocity
this._x = Math.max(20, Math.min(1500-20, Math.round(this._x+this.xvel)));
_root.vcam._x = Math.max(355, Math.min(1500-355, Math.round(this._x+this.xvel)));
//Set the soldier's y position according to its velocity
this._y = Math.round(this._y+this.yvel);
//Check if there is ground below the soldier
if (grass.getPixel32((this._x-grasscontainer._x), (this._y-grasscontainer._y+1)) != 0) {
//loop through each pixel the soldier has moved vertically, checking for the ground
for (moveup=1; moveup<=this.yvel; moveup++) {
//check if the soldier has dropped below the top of the ground
if (grass.getPixel32((this._x-grasscontainer._x), (this._y-grasscontainer._y-moveup)) == 0) {
//there is a collision, so move the y position up
this._y -= (moveup);
//exit the for loop
break;
}
}
//the soldier has hit the ground, so reset the y velocity
this.yvel = 0;
//the soldier is no longer airborne
this.airborne = 0;
} else {
//else the soldier has not hit the ground, so it is still airborne
this.airborne = 1;
}

//Left or A key is pressed, so move the soldier left
if (Key.isDown(Key.LEFT) || Key.isDown(65)) {
this.xvel = -5;
//Else if the right or D key are pressed, move the soldier right
} else if (Key.isDown(Key.RIGHT) || Key.isDown(68)) {
this.xvel = 5;
//Else if neither left or right are pressed, stop the soldier horizontally
} else if (!this.airborne) {
this.xvel = 0;
}
//If the soldier is moving left or right or falling, check if the ground is present
if (this.xvel != 0 && !this.airborne) {
//This moves the soldier up slightly when moving left or right when the ground gets higher
for (buffup=1; buffup<6; buffup++) {
//It helps to keep the soldier on top of the grass at different heights
if (grass.getPixel32((this._x-grasscontainer._x), (this._y-grasscontainer._y-buffup)) == 0) {
//If the soldier is just below the ground, move the soldier up to the ground level
this._y -= buffup;
//soldier is now at the ground level, so break the loop
break;
}
}
//soldier is below the ground level
if (buffup == 6) {
//move the soldier up one pixel to keep him towards the ground level
this._y -= 1;
}
}

};
//This function adds an explosion to the screen at the specified position, also shakes the screen
function explosion(xpos, ypos) {
//attach the explosion
this.attachMovie('explosion','explosion'+totalobjects,this.getNextHighestDepth());
//set the x and y positions
eval('explosion'+totalobjects)._x = xpos;
eval('explosion'+totalobjects)._y = ypos;
//increment the total number of objects on the screen
totalobjects++;

// Now shake the screen
origX = vcam._x + stage.width/2;
origY = 0;

// Set the ammount of shakes
shakeCount = 0;

this.onEnterFrame = function() {
temp = Math.random(2);
if (temp == 0) {
temp = -1;
}
this._rotation = random(3)*temp;
this._x = random(3)+origX;
this._y = random(6)+origY;
shakeCount++;
if (shakeCount == 10) {
shakeCount = 0;
delete this.onEnterFrame;
this._x = origX;
this._y = origY;
this._rotation = 0;
}
};


}
//This function erases a circle of the specified size from the specified bitmap
function circleErase(bitmap, xpos, ypos, circlesize) {
//r2 is the radius of the circle squared. Keeps the math out of the for loop
r2 = circlesize*circlesize;
//The circle is drawn using a series of vertical lines.
for (circx=-circlesize; circx<=circlesize; circx++) {
//Determine the y distance at the current x position.
//The y is going to be larger as it gets closer to the center of the circle
circy = Math.round(Math.sqrt(r2-circx*circx)+0.5);
//Draw a transparent vertical line using the start x and y positions,
//with height according to its distance from the center
bitmap.fillRect(new Rectangle(xpos+circx, ypos-circy, 1, circy*2),0x0);
}
}
//The game has been lost, so make a bunch of explosions at the bottom of the screen
//and delete all of the enemies
function loseGame() {
//Create a bunch of explosions at the bottom of the screen
for (xpos=10; xpos<Stage.width; xpos=xpos+20+Math.random()*40) {
//move along the x axis, adding explosions every 20+Math.random()*40 pixels
explosion(xpos,Math.round(Stage.height-35+Math.random()*30));
}
//clear all enemies from the screen
removeAllEnemies();
//this prevents new enemies from appearing
gameon = 0;
//if the loser screen is not currently up, attach it and center it
if (!loser) {
//attach the loser/play again button
this.attachMovie('loser','loser',this.getNextHighestDepth());
//centerit
loser._x = Stage.width/2;
loser._y = Stage.height/2;
//when the button is pressed, start a new game
loser.onPress = function() {
//start making new planes
gameon = 1;
//fill all blown up grass back in
resetGrass();
//reset the score
score.score = 0;
//reset the level, changing the frequency of the planes and bombs
curlevel = 1;
//reset the number of passed planes. curlevel increments when a certain number of planes have passed
passedplanes = 0;
//a new game begins, so remove the play again button
removeMovieClip(this);
};
}
}
//Each time the player clicks the mouse, a rock is thrown towards the mouse
onMouseDown = function () {
//attach the rock
this.attachMovie('interceptor','interceptor'+totalobjects,this.getNextHighestDepth());
//set the x and y positions at the position of the soldier
eval('interceptor'+totalobjects)._x = soldier._x;
eval('interceptor'+totalobjects)._y = soldier._y-20;
//The velocities are set according to the angle of the mouse relative to the soldier
//X velocity of the rock. More x velocity means less y velocity
eval('interceptor'+totalobjects).xvel = 13*(_xmouse-soldier._x)/(Math.abs(_xmouse-soldier._x)+Math.abs(_ymouse-soldier._y));
//Y velocity of the rock. More y velocity means less x velocity
eval('interceptor'+totalobjects).yvel = 13*(_ymouse-soldier._y)/(Math.abs(_xmouse-soldier._x)+Math.abs(_ymouse-soldier._y));
//At each frame, move the rock according to its velocity and check for enemy collisions
eval('interceptor'+totalobjects).onEnterFrame = function() {
//Move the x and y positions according to the velocity
this._x += this.xvel;
this._y += this.yvel;
//check if this rock collides with any enemies
enemyCollision(this._name);
//if the rock has left the boundaries of the Stage, remove it
if (this._x<0 || this._x>1500 || this._y<0 || this._y>Stage.height) {
removeMovieClip(this);
}
};
//Increment the total number of objects created
totalobjects++;

};
//Run at each frame. If a game is in progress, attach enemies at semi-random intervals,
//generating enemies at a faster rate as the game progresses
this.onEnterFrame = function() {
//If a game is in progress and the random number is lower than a certain amount, generate a plane
if (gameon && (Math.random()*20)/curlevel<1) {
//add the plane to the array of enemies
enemyarray.push('plane'+totalobjects);
//attach the actual plane
attachMovie('plane','plane'+totalobjects,this.getNextHighestDepth());
//movedir is either 0 or -1. When 0, start _x at 0.
//when -1, start _x at Stage.width
eval('plane'+totalobjects).movedir = Math.floor(Math.random()*2)-1;
//Set the plane's y position near the top
eval('plane'+totalobjects)._y = Math.random()*50+15;
//Evaluates to either 0 or Stage.width
eval('plane'+totalobjects)._x = 1500*(-eval('plane'+totalobjects).movedir);
//If the plane starts on the right hand side of the screen, flip it horizontally
//so it appears to be flying in the right direction
eval('plane'+totalobjects)._xscale = 100*(1+2*eval('plane'+totalobjects).movedir);
//If the plane starts at the right of the screen, move it towards the left, otherwise
//move to to the right. If it is past the Stage in either direction, remove it
eval('plane'+totalobjects).onEnterFrame = function() {
//Move the x position according to its direction
this._x += 1+this.movedir*2;
//If the plane has past the Stage boundaries, remove it
if ((Stage.width<this._x && this.movedir == 0) || (this._x<0 && this.movedir == -1)) {
removeEnemy(this._name);
}
//If the random number is below a certain amount, attach a bomb
if ((Math.random()*500)/(curlevel*2)<1) {
//Add the bomb to the list of enemies
enemyarray.push('bomb'+totalobjects);
//Attach the bomb
this._parent.attachMovie('bomb','bomb'+totalobjects,this._parent.getNextHighestDepth());
//set the x and y positions to the position of the plane
eval('this._parent.bomb'+totalobjects)._x = Math.round(this._x);
eval('this._parent.bomb'+totalobjects)._y = Math.round(this._y+20);
//at each frame, move the bomb and check if it hits the grass or the bottom of the screen
eval('this._parent.bomb'+totalobjects).onEnterFrame = function() {
//the bomb moves 5 pixels downwards at each frame, so check for collisions
for (checkrow=1; checkrow<=5; checkrow++) {
//if the bomb hits the grass, blow up a piece of the grass and remove the bomb
if (grass.getPixel32((this._x-grasscontainer._x), (this._y-grasscontainer._y+checkrow)) != 0 || grass.getPixel32((this._x-grasscontainer._x-5), (this._y-grasscontainer._y+checkrow)) != 0 || grass.getPixel32((this._x-grasscontainer._x+5), (this._y-grasscontainer._y+checkrow)) != 0) {
//erase a hole in the grass at the specified position and size
circleErase(grass,this._x-grasscontainer._x,this._y-grasscontainer._y+checkrow,15);
//create an explosion at the specified position
explosion(Math.round(this._x),Math.round(this._y));
//remove the bomb
removeEnemy(this._name);
}
}
//Move the bomb downwards
this._y += 5;
//if the bomb hits the bottom of the screen, lose the game
if (this._y>Stage.height) {
//run the losegame function, which makes a line of bombs at the floor and stops the planes from appearing
loseGame();
//remove the bomb
removeEnemy(this._name);
}
};
//increment the total number of objects created
totalobjects++;
}
};
//increment the total number of objects
totalobjects++;
//increment the number of passed planes
passedplanes++;
//if the number of passed planes hits 40, increment the level
//more planes and bombs appear at higher levels
if (passedplanes>40) {
//reset the number of planes
passedplanes = 0;
//increment the level
curlevel++;
}
}


};
onEnterFrame = function() {
updateRadar();
}

If I remove

doTrail(_root, soldier._x, soldier._y, currentEffect);

then the game runs fine.

Can anyone tell me how to attach the smoke to my character?

I can upload demos if you want
(I removed some comments to fit the 20k limit)

gnomeontherun
02-18-2009, 10:03 AM
Have you tried just having the doTrail() outside of the function?

pradvan
02-25-2009, 03:47 PM
You were most likely having problems with depth management. You were creating new smoke particles on root that were replacing everything else in your game.

Take a look at the attached fla, it fixes your problem by using getNextHighestDepth()

-PR

alihan1988
03-28-2009, 01:13 PM
thanks. good codes



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum