Copy link to clipboard
Copied
This is the code for my android game I am trying to make:
import flash.events.MouseEvent;
import flash.media.SoundChannel;
import flash.display.MovieClip;
var STATE_INIT_GAME:String = "STATE_INIT_GAME";
var STATE_PLAY_GAME:String = "STATE_PLAY_GAME";
var STATE_END_GAME:String = "STATE_END_GAME";
var gameState:String;
var hearts:Array;
var atoms:Array;
var bombs:Array;
var enemies:Array;
var level:Number;
var score:Number;
var lives:Number;
var tempHeart:MovieClip;
var tempBomb:MovieClip;
var tempAtom:MovieClip;
var tempEnemy:MovieClip;
var rSound:rMusic = new rMusic ;
var Rchannel:SoundChannel;
var offset:int = 20;
var enemyBaseSpeed:int = 4;
var speedLevelInc:Number = 3;
var MAX_SPEED:Number = 100;
score = 0;
roachLevel.score_txt.text = String(score);
function gameLoopR(e:Event😞void
{
switch (gameState)
{
case STATE_INIT_GAME :
initGame();
break;
case STATE_PLAY_GAME :
playGame();
break;
case STATE_END_GAME :
endGame();
break;
}
}
function initGame():void
{
Rchannel = rSound.play(0,9999);
level = 1;
roachLevel.level_txt.text = String(level);
lives = 3;
roachLevel.lives_txt.text = String(lives);
hearts = new Array();
bombs = new Array();
atoms = new Array();
enemies = new Array();
gameState = STATE_PLAY_GAME;
}
function playGame():void
{
makeEnemies();
moveEnemies();
makeHearts();
moveHearts();
makeBombs();
moveBombs();
makeAtoms();
moveAtoms();
testForEnd();
}
function makeBombs():void
{
var chance:Number = Math.floor(Math.random() * 6000);
if (chance <= + level)
{
tempBomb.scaleX = 1.5;
tempBomb.scaleY = 1.5;
tempBomb = new Bomb();
tempBomb.x = Math.round(Math.random() * 480);
tempBomb.cacheAsBitmap = true;
addChild(tempBomb)
bombs.push(tempBomb);
tempBomb.speed = 1;
}
}
function moveBombs():void
{
var tempBomb:MovieClip;
for (var h:int =bombs.length-1; h>=0; h--)
{
tempBomb = bombs[h];
if (tempBomb.dead)
{
Rchannel.stop();
lives = 0;
roachLevel.level_txt.text = String(lives);
bombs.splice(h,1);
}
else
{
tempBomb.rotation += (Math.round(Math.random()*.4));
tempBomb.y += (Math.cos((Math.PI/180)*tempBomb.rotation))*tempBomb.speed;
if (tempBomb.x < 10)
{
tempBomb.x = 11;
}
if (tempBomb.x > stage.stageWidth - offset)
{
tempBomb.x = stage.stageWidth - offset;
}
if (tempBomb.y > stage.stageHeight)
{
removeBomb(h);
}
}
}
}
function makeEnemies():void
{
var chance:Number = Math.floor(Math.random() * 150);
if (chance <= level && enemies.length < 4)
{
tempEnemy = new Enemy();
tempEnemy.x = Math.round(Math.random() * 480);
tempEnemy.cacheAsBitmap = true;
addChild(tempEnemy);
tempEnemy.scaleX = 1.5;
tempEnemy.scaleY = 1.5;
enemies.push(tempEnemy);
tempEnemy.speed = enemyBaseSpeed + ((level - 1) * speedLevelInc);
if (tempEnemy.speed > MAX_SPEED)
{
tempEnemy.speed = MAX_SPEED;
}
}
}
function moveEnemies():void
{
var tempEnemy:MovieClip;
for (var i:int =enemies.length-1; i>=0; i--)
{
tempEnemy = enemies[i];
if (tempEnemy.dead)
{
score++;
score++;
roachLevel.score_txt.text = String(score);
enemies.splice(i,1);
}
else
{
tempEnemy.rotation += (Math.round(Math.random()*.4));
tempEnemy.y += (Math.cos((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed;
if (tempEnemy.x < 10)
{
tempEnemy.x = 11;
}
if (tempEnemy.x > stage.stageWidth - offset)
{
tempEnemy.x = stage.stageWidth - offset;
}
if (tempEnemy.y > stage.stageHeight)
{
removeEnemy(i);
lives--;
roachLevel.lives_txt.text = String(lives);
}
}
}
}
function makeHearts():void
{
var chance:Number = Math.floor(Math.random() * 8000);
if (chance <= + level)
{
tempHeart = new Heart();
tempHeart.x = Math.round(Math.random() * 480);
tempHeart.cacheAsBitmap = true;
addChild(tempHeart);
tempHeart.scaleX = 1.5;
tempHeart.scaleY = 1.5;
hearts.push(tempHeart);
tempHeart.speed = enemyBaseSpeed + ((level - 1) * speedLevelInc);
}
}
function moveHearts():void
{
var tempHeart:MovieClip;
for (var k:int =hearts.length-1; k>=0; k--)
{
tempHeart = hearts[k];
if (tempHeart.dead)
{
lives++;
roachLevel.lives_txt.text = String(lives);
hearts.splice(k,1);
}
else
{
tempHeart.rotation += (Math.round(Math.random()*.4));
tempHeart.y += (Math.cos((Math.PI/180)*tempHeart.rotation))*tempHeart.speed;
if (tempHeart.x < 10)
{
tempHeart.x = 11;
}
if (tempHeart.x > stage.stageWidth - offset)
{
tempHeart.x = stage.stageWidth - offset;
}
if (tempHeart.y > stage.stageHeight)
{
removeHeart(k);
}
}
}
}
function makeAtoms():void
{
var chance:Number = Math.floor(Math.random() * 7500);
if (chance <= + level)
{
tempAtom = new Atom();
tempAtom.x = Math.round(Math.random() * 480);
tempAtom.cacheAsBitmap = true;
addChild(tempAtom);
tempAtom.scaleX = 1.5;
tempAtom.scaleY = 1.5;
atoms.push(tempAtom);
tempAtom.speed = enemyBaseSpeed + ((level - 1) * speedLevelInc);
}
}
function moveAtoms():void
{
var tempAtom:MovieClip;
for (var c:int =atoms.length-1; c>=0; c--)
{
tempAtom = atoms[c];
if (tempAtom.dead)
{
score++;
score++;
score++;
score++;
score++;
roachLevel.score_txt.text = String(score);
atoms.splice(c,1);
}
else
{
tempAtom.rotation += (Math.round(Math.random()*.4));
tempAtom.y += (Math.cos((Math.PI/180)*tempAtom.rotation))*tempAtom.speed;
if (tempAtom.x < 10)
{
tempAtom.x = 11;
}
if (tempAtom.x > stage.stageWidth - offset)
{
tempAtom.x = stage.stageWidth - offset;
}
if (tempAtom.y > stage.stageHeight)
{
removeAtom(c);
}
}
}
}
function removeEnemy(id:int)
{
removeChild(enemies[id]);
enemies.splice(id,1);
}
function removeHeart(kd:int)
{
removeChild(hearts[kd]);
hearts.splice(kd,1);
}
function removeBomb(hd:int)
{
removeChild(bombs[hd]);
bombs.splice(hd,1);
}
function removeAtom(cd:int)
{
removeChild(atoms[cd]);
atoms.splice(cd,1);
}
function testForEnd():void
{
if (score > level * 20)
{
level++;
roachLevel.level_txt.text = String(level);
}
if (lives == 0)
{
gameState = STATE_END_GAME;
}
}
function endGame():void
{
removeGame();
roachLevel.visible = false;
Menu_mc.visible = false;
endscreen_mc.visible = true;
removeEventListener(Event.ENTER_FRAME, gameLoopR);
showresults();
}
function removeGame():void
{
for (var i:int = enemies.length-1; i >=0; i--)
{
removeEnemy(i);
}
for (var h:int = bombs.length-1; h >=0; h--)
{
removeBomb(h);
}
for (var k:int = hearts.length-1; k >=0; k--)
{
removeHeart(k);
}
for (var c:int = atoms.length-1; c >=0; c--)
{
removeAtom(c);
}
}
The game works perfectly on Android and everything works. There is one problem though. The game gets laggy and the enemies get harder to click when the enemies' speed increases. This is the code for the enemies. It is a movie clip that gets added on to the stage by actionscript.
import flash.events.MouseEvent;
import flash.display.MovieClip;
import fl.motion.Animator;
import flash.events.*;
play();
var mysound:squish = new squish();
this.addEventListener(MouseEvent.CLICK, kill);
this.dead = false;
function kill(e:MouseEvent😞void
{
this.dead=true;
mouseChildren=false
mysound.play();
gotoAndPlay(21);
this.removeEventListener(MouseEvent.CLICK, kill);
flash.utils.setTimeout(removeSelf,2000);
}
function removeSelf():void
{
this.parent.removeChild(this);
}
no,
var identityMatrix:Matrix=new Matrix()
my_sprite.cacheAsBitmapMatrix=identityMatrix;
though for tempAtom,
var scaleMatrix:Matrix=identityMatrix.scale(1.5,1.5); //would be appropriate.
you can get a cc trial, http://gaming.adobe.com/technologies/scout/
Copy link to clipboard
Copied
you should profile your game using adobe scout to see why it gets laggy.
but i do see that you're using cacheAsBitmap inappropriately on objects that then get scaled and/or rotated and possibly otherwise transformed.
you should be assigning their cacheAsBitmapMatrix.
Copy link to clipboard
Copied
Is this how I do the caching: my_sprite.cacheAsBitmapMatrix = true; ? I cannot download Adobe Scout off of creative cloud because of my older operating system. Is there an older version I can download for free to test it out?
Copy link to clipboard
Copied
no,
var identityMatrix:Matrix=new Matrix()
my_sprite.cacheAsBitmapMatrix=identityMatrix;
though for tempAtom,
var scaleMatrix:Matrix=identityMatrix.scale(1.5,1.5); //would be appropriate.
you can get a cc trial, http://gaming.adobe.com/technologies/scout/
Copy link to clipboard
Copied
Yeah you have to do everything in bitmaps if you're making games or apps and especially if you're making a game app.
You can use regular flash clips, but you have to convert them to bitmaps before you use them. Remember this important point, always check for the devices max bitmap dimensions - usually the size closest to the screen dimensions.
There are great tutorials on app creation, bitmaps, game dev, etc. at gotoandlearn.com
Find more inspiration, events, and resources on the new Adobe Community
Explore Now