文档章节

Just A Backup

童年伊梦
 童年伊梦
发布于 2012/09/25 12:25
字数 1296
阅读 23
收藏 0
as3
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.media.Sound;
import flash.net.SharedObject;
     
/**************VARIABLES**************/
var STATE_INIT_GAME:String = "STATE_INIT_GAME";
var STATE_START_PLAYER:String = "STATE_START_PLAYER";
var STATE_PLAY_GAME:String = "STATE_PLAY_GAME";
var STATE_END_GAME:String = "STATE_END_GAME";
var gameState:String;
     
var player:MovieClip;
var enemies:Array;
var level:Number;
var score:Number;
var lives:Number;
var accel:Accelerometer;
var Lasers:Array;
var explosions:Array;
var hiddenOptions:Boolean = true;
/**************SETUP**************/
     
endScreen.visible = false;
optionsMenu.visible = false;
     
/**************INTRO SCREEN**************/
     
introScreen.play_btn.addEventListener(MouseEvent.CLICK, clickAway);
function clickAway(event:MouseEvent):void
{
    moveScreenOff(introScreen);
}
//Gesture Swipe, zoom, rotate, pan
Multitouch.inputMode = MultitouchInputMode.GESTURE;
introScreen.addEventListener(TransformGestureEvent.GESTURE_SWIPE, swipeAway);
function swipeAway(event:TransformGestureEvent):void
{
    //Swipe Left (-1) Right(1), offsetY(-1), offsetY(1) down.
    if (event.offsetX == -1)
    {
        //Do something
        moveScreenOff(introScreen);
    }
}
     
     
function moveScreenOff(screen:MovieClip):void
{
    //Move the screen off...
    var introTween = new Tween(screen,"x",Strong.easeInOut,screen.x,(screen.width)*-1,1,true);
    //When the motion has finished...
    introTween.addEventListener(TweenEvent.MOTION_FINISH, tweenFinish);
    function tweenFinish(e:TweenEvent):void
    {
        trace("tweenFinish");
        //Establish the game state...
        gameState = STATE_INIT_GAME;
        trace(gameState);
        //Fire off the gameLoop function at the frame rate of the movie...
        addEventListener(Event.ENTER_FRAME, gameLoop);
    }
}
     
/**************GAME STATES**************/
function gameLoop(e:Event):void
{
    //Keep track of the gameState 
    switch (gameState)
    {
        case STATE_INIT_GAME :
            initGame();
            break;
        case STATE_START_PLAYER :
            startPlayer();
            break;
        case STATE_PLAY_GAME :
            playGame();
            break;
        case STATE_END_GAME :
            endGame();
            break;
    }
}
     
     
/**************STATE_INIT_GAME**************/
function initGame():void
{
     
    level = 1;
    level_txt.text = String(level);
    score = 0;
    score_txt.text = String(score);
    lives = 2;
    lives_txt.text = String(lives);
     
    //Set up all graphics...
    //Pull in the Player from the library...
    player = new Player();
    //Create an enemies array (a big box that can hold many things)
    enemies = new Array();
    //Create an array to hold all lasers
    Lasers = new Array();
    //Create an array called explosions
    explosions = new Array();
    gameState = STATE_START_PLAYER;
    trace(gameState);
     
}
     
/**************STATE_START_PLAYER**************/
function startPlayer():void
{
    //Create the player, properties...
    player.y = stage.stageHeight - player.height;
    player.cacheAsBitmap = true;
    addChild(player);
    //Start firing the laser
    laserTimer.start();
    //Create an accelerometer;
    accel = new Accelerometer();
    //If the phone has accelerometer support...
    if (Accelerometer.isSupported)
    {
        //As the user tilts their device...
        accel.addEventListener(AccelerometerEvent.UPDATE, accelMove);
    }
    else
    {
        //If there is no accelerometer support...
        addEventListener(Event.ENTER_FRAME, movePlayer);
    }
    gameState = STATE_PLAY_GAME;
    trace(gameState);
}
     
function accelMove(event:AccelerometerEvent):void
{
    //accelerationX returns a number between 0,1. Properties: Y, Z.
    player.x -=  event.accelerationX * 80;
    if (player.x < 0)
    {
        player.x = 0;
    }
    else if (player.x > (stage.stageWidth - player.width) )
    {
        player.x = stage.stageWidth - player.width;
    }
}
     
     
function movePlayer(e:Event):void
{
    player.x = stage.mouseX;
    //Doesn't go off the right or left side...
    if (player.x < 0)
    {
        player.x = 0;
    }
    else if (player.x > (stage.stageWidth - player.width) )
    {
        player.x = stage.stageWidth - player.width;
    }
}
     
//Create a timer that executes a function every 1/2 second
var laserTimer:Timer = new Timer(500);
laserTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener(e:TimerEvent):void
{
    //In the Library linkage for a movieclip should be Laser
    var tempLaser:MovieClip = new Laser();
    //Place the laser at the tip of the player's ship
    tempLaser.x = player.x +(player.width/2);
    tempLaser.y = player.y;
    tempLaser.cacheAsBitmap = true;
    tempLaser.speed = 10;
    Lasers.push(tempLaser);
    addChild(tempLaser);
}
     
//Lasers array is required
function moveLaser():void
{
    //Move Laser
    var tempLaser:MovieClip;
    for (var i=Lasers.length-1; i>=0; i--)
    {
        tempLaser = Lasers[i];
        //Move the laser up based on the speed defined...
        tempLaser.y -=  tempLaser.speed;
        if (tempLaser.y < 0)
        {
            //Remove laser 1 from Lasers array
            removeLaser(i);
        }
    }
    //Remove any tempExplosion from the explosions array that has already played
    var tempExplosion:MovieClip;
    for (i=explosions.length-1; i>=0; i--)
    {
        tempExplosion = explosions[i];
        if (tempExplosion.currentFrame >= tempExplosion.totalFrames)
        {
            removeExplosion(i);
        }
    }
}
     
     
/**************STATE_PLAY_GAME**************/
function playGame():void
{
    //Set up the enemies, explosions etc.
     
    makeEnemies();
    moveEnemies();
    moveLaser();
    testForEnd();
}
     
//Call this function for how many enemies you want to make...
function makeEnemies():void
{
    //Pick a number between 0-60
    var chance:Number = Math.floor(Math.random() * 60);
    //If the number is less than or equal to 1+level (2+)
    if (chance <= 1 + level)
    {
        var tempEnemy:MovieClip;
        //Make sure a Library item linkage is set to Enemy...
        tempEnemy = new Enemy();
        tempEnemy.speed = 3;
        //Math.random(); gets a random number from 0.0-1.0
        tempEnemy.x = Math.round(Math.random() * 800);
        tempEnemy.cacheAsBitmapMatrix = tempEnemy.transform.concatenatedMatrix;
        tempEnemy.cacheAsBitmap = true;
        trace("tempEnemy");
        addChild(tempEnemy);
        enemies.push(tempEnemy);
    }
}
//Create an enemies array if you do not already have one...
//Create tempEnemy from Enemy library item if you do not already have one...
function moveEnemies():void
{
    var tempEnemy:MovieClip;
    for (var i:int =enemies.length-1; i>=0; i--)
    {
        tempEnemy = enemies[i];
     
        //rotate the enemy between 10-5 degrees
        tempEnemy.rotation += (Math.round(Math.random()*10-5));
        //Find the rotation and move the x position that direction
        tempEnemy.x -=  (Math.sin((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed;
        tempEnemy.y +=  (Math.cos((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed;
        if (tempEnemy.x < 0)
        {
            tempEnemy.x = 0;
        }
        if (tempEnemy.x > stage.stageWidth)
        {
            tempEnemy.x = stage.stageWidth;
        }
        if (tempEnemy.y > stage.stageHeight || tempEnemy.hitTestObject(player))
        {
            makeExplosion(tempEnemy.x, tempEnemy.y);
            //Remove enemy from enemies array
            removeEnemy(i);
            //Subtract a life
            lives--;
            lives_txt.text = String(lives);
        }
        //Make Explosion...
        var tempLaser:MovieClip;
        tempEnemy = enemies[i];
        //Loop through the Lasers array
        for (var j:int=Lasers.length-1; j>=0; j--)
        {
            tempLaser = Lasers[j];
            if (tempEnemy.hitTestObject(tempLaser))
            {
                makeExplosion(tempEnemy.x, tempEnemy.y);
                //Remove enemy from enemies array
                removeEnemy(i);
                //Remove laser from Lasers array
                removeLaser(j);
                //Add to the score
                score++;
                score_txt.text = String(score);
            }
        }
    }
}
     
     
     
     
function makeExplosion(ex:Number, ey:Number):void
{
    var tempExplosion:MovieClip;
    tempExplosion = new Explosion();
    tempExplosion.x = ex;
    tempExplosion.y = ey;
    addChild(tempExplosion);
    explosions.push(tempExplosion);
    //Play an explosion sound;
    var sound:Sound = new Explode();
    sound.play();
}
     
function testForEnd():void
{
    //
    if (score > level * 10)
    {
        level++;
        level_txt.text = String(level);
    }
    //Check to see how many lives the user has
    if (lives == 0)
    {
        gameState = STATE_END_GAME;
        trace(gameState);
    }
}
     
function removeEnemy(idx:int)
{
    removeChild(enemies[idx]);
    enemies.splice(idx,1);
}
     
     
function removeLaser(idx:int)
{
    removeChild(Lasers[idx]);
    Lasers.splice(idx,1);
}
     
function removeExplosion(idx:int)
{
    removeChild(explosions[idx]);
    explosions.splice(idx,1);
}
     
/**************END SCREEN**************/
function endGame():void
{
    removeGame();
    endScreen.visible = true;
    removeEventListener(Event.ENTER_FRAME, gameLoop);
    showResults();
}
/**************REMOVE GAME**************/
function removeGame():void
{
    trace("Remove Game");
    //Loop through an enemies array
    for (var i:int = enemies.length-1; i >=0; i--)
    {
        removeEnemy(i);
    }
    //Remove lasers
    for (var j:int = Lasers.length-1; j >=0; j--)
    {
        removeLaser(j);
    }
    //Explosions
    for (var k:int = explosions.length-1; k >=0; k--)
    {
        removeExplosion(k);
    }
    //Remove the player
    removeChild(player);
    laserTimer.stop();
}
     
     
function showResults():void
{
    trace("Show Results");
    //Hides the text field and enter button...
    endScreen.enter_btn.visible = false;
    endScreen.highScoreName.visible = false;
    //Create a shared object that will store the all time high score
    var so:SharedObject = SharedObject.getLocal("alltimeHighScore");
    //Show results
    //If the score is greater than zero
    if (so.data.score == undefined || score > so.data.score)
    {
        endScreen.highScore.text = "You made it to level " + level + " with a high score of " + score + ". \n Enter your name below.";
        //Turns on the enter button and the input text field..
        endScreen.enter_btn.visible = true;
        endScreen.highScoreName.visible = true;
    }
    else
    {
        //If the player didn't get the high score...
        endScreen.highScore.text = "Your score of " + score + " does not beat " + so.data.score + " by " + so.data.name + ".";
    }
    //Turning on and making the enter button "work"
    endScreen.enter_btn.addEventListener(MouseEvent.CLICK, clickEnter);
    function clickEnter(event:MouseEvent):void
    {
        trace("clickEnter");
        endScreen.highScore.text = "Great job, " + endScreen.highScoreName.text + "! \n You made it to " + level + " with a score of " + score + "!";
        //Define the properties of the shared object (so)
        so.data.score = score;
        so.data.level = level;
        so.data.name = endScreen.highScoreName.text;
        so.flush();
        //Hides the enter button just selected and the input text field...
        endScreen.enter_btn.visible = false;
        endScreen.highScoreName.visible = false;
    }
    //Enables the play button
    endScreen.play_btn.addEventListener(MouseEvent.CLICK, clickFinalAway);
    function clickFinalAway(event:MouseEvent):void
    {
        trace("clickFinalAway");
        moveScreenOff(endScreen);
    }
}
     
/**************OPTIONS MENU**************/
//If any hardware key is pressed...
stage.addEventListener(KeyboardEvent.KEY_UP, optionsKey);
function optionsKey(event:KeyboardEvent):void
{
    //options key/menu 95
    //The enter key is 13
    if (event.keyCode == 95 || event.keyCode == 13)
    {
        //If it's set to true...
        if (hiddenOptions)
        {
            //Put the optionsMenu on top of everything...
            setChildIndex(optionsMenu,numChildren-1);
            optionsMenu.visible = true;
            optionsMenu.addEventListener(MouseEvent.CLICK, exitApp);
            pauseGame();
        }
        else
        {
            //If the optionsMenu is showing then hide it...
            optionsMenu.visible = false;
            optionsMenu.removeEventListener(MouseEvent.CLICK, exitApp);
            resumeGame();
        }
        //Toggle...
        hiddenOptions = ! hiddenOptions;
    }
}
     
function exitApp(event:MouseEvent):void
{
    trace("exitApp");
    //UNCOMMENT ON PUBLISH
    //AIR functionality
    //NativeApplication.nativeApplication.exit(0);
}
     
//UNCOMMENT ON PUBLISH - SET THE NECESSARY PERMISSIONS...
//AIR for Android devices - Keep screen awake if you are using the Accelerometer etc.
//NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
     
     
     
     
     
     
     
     
     
//When the user gets a phone call, or launches another app...
stage.addEventListener(Event.DEACTIVATE, Deactivate);
function Deactivate(event:Event):void
{
    pauseGame();
}
//When the user comes back to the game...
stage.addEventListener(Event.ACTIVATE, Activate);
function Activate(event:Event):void
{
    resumeGame();
}
     
function pauseGame():void
{
    //Remove any listener events, timers etc.
    if (gameState == STATE_PLAY_GAME)
    {
        removeEventListener(Event.ENTER_FRAME, gameLoop);
        laserTimer.stop();
        accel.removeEventListener(AccelerometerEvent.UPDATE, accelMove);
    }
}
     
function resumeGame():void
{
    //Activate any listener events, timers etc.
    if (gameState == STATE_PLAY_GAME)
    {
        addEventListener(Event.ENTER_FRAME, gameLoop);
        laserTimer.start();
        accel.addEventListener(AccelerometerEvent.UPDATE, accelMove);
    }
}


© 著作权归作者所有

童年伊梦
粉丝 1
博文 29
码字总数 21736
作品 0
浦东
私信 提问
Backup Volume 操作 - 每天5分钟玩转 OpenStack(59)

本节我们讨论 volume 的 Backup 操作。 Backup 是将 volume 备份到别的地方(备份设备),将来可以通过 restore 操作恢复。 Backup VS Snapshot 初看 backup 功能好像与 snapshot 很相似,都...

CloudMAN
2016/07/13
216
0
SQL备份语句

--完整备份 Backup Database NorthwindCS To disk='G:\Backup\NorthwindCS_Full_20070908.bak' --差异备份 Backup Database NorthwindCS To disk='G:\Backup\NorthwindCS_Diff_20070908.bak......

长平狐
2012/09/06
40
0
RocksDB系列十一:How to backup RocksDB?

Backup API c++ api,请参考:include/rocksdb/utilities/backupable_db.h。核心模块是backup engine,其暴露了创建backup的简单接口、查询backup的相关信息以及从backup中恢复数据。backup ...

薛少佳
2018/07/19
0
0
How to Backup a Computer

How to Backup a Computer These days, an increasing number of people are using computer to store memories, important documents, and various other precious information which need ......

郝槟楠
2018/07/03
0
0
Oracle 冷备份详解【实战案例】

Oracle 冷备份详解 --准备工作 select from v$database; select filename from dbadata_files; create tablespace inv datafile '/oradata/ocm/inv01.dbf' size 2048M; create user inv iden......

PengChonggui
2018/06/28
0
0

没有更多内容

加载失败,请刷新页面

加载更多

进程命令ps

命令ps 用途: 显示当前系统的进程状态 语法: ]# ps [选项] 常用选项: aux = 列出正在运行的所有进程; a 显示现行终端机下的所有程序,包括其他用户的程序 u 以用户为主...

迷失De挣扎
今天
3
0
聊聊spring cloud的CachingSpringLoadBalancerFactory

序 本文主要研究一下spring cloud的CachingSpringLoadBalancerFactory CachingSpringLoadBalancerFactory spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/o......

go4it
昨天
4
0
一篇文章搞定——JDK8中新增的StampedLock

一、StampedLock类简介 StampedLock类,在JDK1.8时引入,是对读写锁ReentrantReadWriteLock的增强,该类提供了一些功能,优化了读锁、写锁的访问,同时使读写锁之间可以互相转换,更细粒度控...

须臾之余
昨天
4
0
Android Camera原理之CameraDeviceCallbacks回调模块

在讲解《Android Camera原理之openCamera模块(二)》一文的时候提到了CameraDeviceCallbacks回调,当时没有详细展开,本文我们详细展开讲解一下。 CameraDeviceCallbacks生成过程: 《Android...

天王盖地虎626
昨天
5
0
Python3正则表达式

在Python中使用正则表达式 Python语言通过标准库中的 re模块 (import re)支持正则表达式。 使用 match 方法匹配字符串 匹配字符串也就是设定一个文本模式,然后判断另外一个字符串是否符合这...

ZeroBit
昨天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部