• Главная
  • Блог
  • Пользователи
  • Форум
  • Литературное творчество
  • Музыкальное творчество
  • Научно-техническое творчество
  • Художественно-прикладное творчество

Создание игрового flash-приложения

Опубликовано Шуплецов Иван Геннадьевич вкл 27.10.2013 - 17:51
Шуплецов Иван Геннадьевич
Автор: 
Лукьянов Никита

Создание игрового flash-приложения

Скачать:

ВложениеРазмер
Файл Создание игрового flash-приложения2.74 МБ

Предварительный просмотр:

package {

        import flash.display.*;

        import flash.text.*;

        import flash.utils.setInterval;

        import flash.utils.clearInterval;

        import flash.events.*;

        import flash.geom.*;

        import flash.net.*;

        import flash.ui.*;

        import flash.system.*;

        import vk.*;

        import com.*;

        import com.wispagency.display.Loader;

        public class Main extends MovieClip

        {

                public var flashVars:Object;

                public var VK:APIConnection;

                private var t:int = 0;

                public var inGAME:Boolean = false;

                private var Graphic_move:uint;

                public var GAMES1;

                private var copyrate:copyratebox;

                private var b_start:Button_Start;

                public var shadows:Black_screen;

                public var time_life;//ID

                public var shadow_timeID;

                public var Player_lol;

                public var BlowID;

                public var Score:int = 0;

                private var Scoremax:int;

                public var lvlup;

                public var scup;

                public var TimedataID;

                private var initialized:Boolean = false;

                private var _player:Player;

                private var _enemies:Array;

                private var _proj:Projectile;

                private var cproj:Boolean = false;

                private var _bb:big_button;

                private var ROUND:int;

                private var LIVES:int;

                private const PROJ_SPEED:Number = 20;

                private var period = 0;

                private var init_delay = 0;

                private var image_loader,image,avdata;

                private const LOCAL_TEST = false;

                public function Main(){

                        Security.allowDomain("*");

                        stage.quality = StageQuality.HIGH;

                        if (! LOCAL_TEST){

                                flashVars = stage.loaderInfo.parameters as Object;

                                if (! flashVars.api_id){

                                        flashVars["api_id"] = 2918459;

                                        flashVars["viewer_id"] = 126619772;

                                        flashVars["sid"] = "1ca6b046af4fb01428f4925e08436a74c32b6f03dd0b39f5e84a63d788c878";

                                        flashVars["secret"] = "gLsbZBjjdktP1gEHHqjp";}

                                VK = new APIConnection(flashVars);

                                VK.api('users.get', { uids:flashVars["viewer_id"], fields: "first_name, last_name, photo" }, net_success, net_fail);

                                VK.api('storage.get', { key:"score"}, StartScore, net_fail);

                        }else{creatinterfaceMM();}

                }

                public function net_fail(data: Object){}

                public function net_success(data: Object){

                        this.hi.text = "Привет, " + data[0].first_name + " " + data[0].last_name;

                        image_loader = new Loader();

                        image = new URLRequest(data[0].photo);

                        avdata = new BitmapData(50,50,true,0x00000000);

                        image_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, avatar_loaded);

                        var lc:LoaderContext = new LoaderContext();

                        lc.checkPolicyFile = true;

                        image_loader.load(image, lc);

                }

                public function avatar_loaded( e:Event ){

                        try{

                                avdata.copyPixels(e.target.content.bitmapData, new Rectangle(0, 0, 50, 50),  new Point(0, 0));

                        }

                        catch (e:SecurityError){}

                        var bd = new Bitmap(avdata);

                        addChild( bd );

                        bd.x = 10;

                        bd.y = 10;

                        creatinterfaceMM();

                }

                public function creatinterfaceMM(){

                        LIVES = 3;

                        ROUND = 1;

                        this.round.text = "";

                        this.score_blank.text = "";

                        if (Scoremax > 0){

                                this.SCO.text = String(Scoremax);

                        }else{

                                this.SCO.text = "0";

                        }

                        this.hi.visible = true;

                        copyrate = new copyratebox();

                        copyrate.x = 718.2;

                        copyrate.y = 31.25;

                        addChild(copyrate);

                        img2.gotoAndStop(1);

                        img1.gotoAndStop(1);

                        this.SCO.alpha = 0;

                        b_start=new Button_Start();

                        b_start.x = 406.5;

                        b_start.y = 324.25;

                        addChild(b_start);

                        b_start.addEventListener(MouseEvent.CLICK, clik_game, false, 0, true);

                        Graf_logo.gotoAndStop(1);

                }

                public function StartScore(data: Object){

                        Scoremax = int(data.toString());

                }

                public function makeScore(data: Object){

                        if (String(data.toString()) == ""){

                                this.SCO.text = "0";

                        }else{

                                this.SCO.text = String(data.toString());

                        }

                        Scoremax = int(data.toString());

                }

                private function SaveScoreGooD(data:Object){}

                private function clik_game(e:MouseEvent){

                        this.hi.visible = false;

                        removeChild(copyrate);

                        copyrate = null;

                        removeChild(b_start);

                        b_start = null;

                        Graf_logo.gotoAndStop(2);

                        img2.gotoAndStop(2);

                        img1.gotoAndStop(2);

                        shadows = new Black_screen();

                        addChild(shadows);

                        t = 3;

                        shadow_timeID = setInterval(timer_del_shadows,1000);

                        time_life = setInterval(life_panel_viewer,10);

                        this.hi.visible = false;

                }

                private function timer_del_shadows(){

                        t--;

                        if (t <= 2 && lvlup){

                                removeChild(lvlup);

                                lvlup = null;

                        }

                        if (t <= 0){

                                removeChild(shadows);

                                shadows = null;

                                clearInterval(shadow_timeID);

                                inGAME = true;

                                this.hi.visible = false;

                                this.SCO.alpha = 1;

                                addEventListener( Event.ENTER_FRAME, _iteration, false, 0, true );

                                addEventListener(KeyboardEvent.KEY_DOWN, keylistener_downkey, false, 0, true);

                        }

                }

                private function life_panel_viewer(){

                        if (inGAME == true){

                                Info_lvl.gotoAndStop(2);

                                this.score_blank.text = String(Score);

                        }else{

                                Info_lvl.gotoAndStop(1);}

                        if (inGAME == false){

                                Life_p.gotoAndStop(1);

                                return;}

                        if (LIVES == 1){

                                Life_p.gotoAndStop(2);}

                        if (LIVES == 2){

                                Life_p.gotoAndStop(3);}

                        if (LIVES == 3){

                                Life_p.gotoAndStop(4);}

                }

                private function _init(){

                        add_player();

                        add_mobs();

                        _bb = new big_button  ;

                        this.bb_container.addChild(_bb);

                        _bb.addEventListener( MouseEvent.CLICK, in_game_click, false, 0, true );

                        this.round.text = String(ROUND);

                        initialized = true;

                }

                private function _iteration( e:Event ){

                        if (init_delay > 0){

                                init_delay--;

                                if (init_delay != 0){

                                        return;

                                }

                        }

                        if (inGAME && ! initialized){

                                _init();}

                        if (initialized){

                                period +=  get_period();

                                handle_player_position();

                                affect_coords();

                                check_intersections();

                        }

                }

                private function handle_player_position(){

                        _player.x=mouseX+ROUND/2*Math.sin(0.25*Number(TimedataID)*9/(LIVES*LIVES));

                        if (_player.x < 50){

                                _player.x = 50;}

                        if (_player.x > 750){

                                _player.x = 750;}

                        Player_lol.x = _player.x;

                        Player_lol.y = _player.y;

                        TimedataID = setInterval(Time_player,100);

                }

                private function Time_player(){}

                private function affect_coords(){

                        for each (var enemy in _enemies){

                                if (! enemy.free){

                                        enemy.link.x = enemy.x + Math.sin(period) * 50;

                                        enemy.link.y = (enemy.y += get_downshift());

                                }

                        }

                        if (cproj){

                                _proj.y -=  PROJ_SPEED;

                                if (_proj.y < 0){

                                        cproj = false;

                                        this.ingame_container.removeChild( _proj );

                                        _proj = null;

                                        clearInterval(BlowID);

                                        BlowID = null;

                                }

                        }

                }

                private function check_intersections(){

                        var px = _player.x;

                        var pw = _player.width / 2;

                        var py = _player.y;

                        var ph = _player.height / 2;

                        for each (var enemy in _enemies){

                                if (! enemy.free){

                                        var ex = enemy.link.x;

                                        var ew = enemy.link.width / 2;

                                        var ey = enemy.link.y;

                                        var eh = enemy.link.height / 2;

                                        if ((ey > py) || (Math.abs(ex-px) < Math.abs(ew+pw) && Math.abs(ey-py) < Math.abs(eh+ph))){

                                                throw_end_round();

                                                return;}

                                        if (cproj){

                                                var jx = _proj.x;

                                                var jy = _proj.y;

                                                var b = new Enemy_dead();

                                                if (Math.abs(ex - jx) < ew && Math.abs(ey - jy) < eh){

                                                        enemy.free = true;

                                                        this.ingame_container.removeChild( enemy.link );

                                                        enemy.link = null;

                                                        scup =int(100*Math.sqrt(ROUND/2)+(2+ROUND)*Math.random());

                                                        Score +=  scup;

                                                        render_score(scup, ex, ey );

                                                        b.x = ex;

                                                        b.y = ey;

                                                        ingame_container.addChild(b);

                                                        cproj = false;

                                                        this.ingame_container.removeChild( _proj );

                                                        _proj = null;

                                                        clearInterval(BlowID);

                                                        BlowID = null;

                                                        if (! check_enemies_amount()){

                                                                return;}

                                                }

                                        }

                                }

                        }

                }

                

                private function render_score(score, X, Y){

                        var Tf =new Scupmove();

                        Tf.scuptf.text = "+ " + String(score);

                        Tf.x = X + 9;

                        Tf.y = Y - 5;

                        ingame_container.addChild(Tf);

                }

                private function check_enemies_amount():Boolean{

                        var free = 0;

                        for each (var enemy in _enemies){

                                if (enemy.free){

                                        free++;}

                        }

                        if (free == 20){

                                throw_end_round( false );

                                return false;}

                        return true;

                }

                private function get_downshift():Number{

                        return 0.1 + (ROUND-1)*0.03;

                }

                private function get_period():Number{

                        return 0.02 + (ROUND-1)*0.005;

                }

                private function throw_end_round( death:Boolean = true, exit:Boolean = false ){

                        clearInterval(TimedataID);

                        TimedataID = null;

                        this.ingame_container.removeChild( _player );

                        _player = null;

                        this.ingame_container.removeChild( Player_lol);

                        Player_lol = null;

                        if (cproj){

                                cproj = false;

                                this.ingame_container.removeChild( _proj );

                                _proj = null;

                                clearInterval(BlowID);

                                BlowID = null;

                        }

                        for each (var enemy in _enemies){

                                if (! enemy.free){

                                        enemy.free = true;

                                        this.ingame_container.removeChild( enemy.link );

                                        enemy.link = null;

                                }

                        }

                        if (death){

                                LIVES--;

                                if (LIVES == 0 || exit){

                                        end_game();

                                        return;}

                        }else{

                                ROUND++;

                                shadows = new Black_screen();

                                addChild(shadows);

                                lvlup= new Level_up();

                                lvlup.x = 430;

                                lvlup.y = 266,2;

                                addChild(lvlup);

                                lvlup.round2.text = String(ROUND);

                                t = 3;

                                shadow_timeID = setInterval(timer_del_shadows,1000);

                        }

                        while (ingame_container.numChildren>0){

                                var child = ingame_container.getChildAt(0);

                                ingame_container.removeChildAt(0);

                                child = null;

                        }

                        initialized = false;

                        init_delay = 36 * 2;

                }

                private function keylistener_downkey(event:KeyboardEvent){

                        if (event.keyCode == 13){

                                throw_end_round( true, true );}

                }

                private function end_game(){

                        initialized = false;

                        inGAME = false;

                        this.hi.visible = true;

                        removeEventListener( Event.ENTER_FRAME, _iteration );

                        removeEventListener(KeyboardEvent.KEY_DOWN, keylistener_downkey );

                        _bb.removeEventListener( MouseEvent.CLICK, in_game_click );

                        this.bb_container.removeChild(_bb);

                        _bb = null;

                        creatinterfaceMM();

                        SaveScore();

                }

                private function SaveScore(){

                        var Scvar;

                        if (LOCAL_TEST){

                                if (Score > Scoremax){

                                        Scoremax = Score;}

                                this.SCO.text = String(Scoremax);

                        }

                        if (! LOCAL_TEST){

                                if ((Score!=0)&&(Score>Scoremax)){

                                        Scvar = String(Score);

                                        VK.api('storage.set', { key:"score", value:Scvar}, SaveScoreGooD, net_fail);

                                        VK.api('storage.set', { key:"score", value:Scvar}, SaveScoreGooD, net_fail);

                                        VK.api('storage.set', { key:"score", value:Scvar}, SaveScoreGooD, net_fail);

                                }

                                VK.api('storage.get', { key:"score"}, makeScore, net_fail);

                        }

                        Score = 0;

                }

                private function add_player(){

                        Player_lol= new lol_effects();

                        Player_lol.x = 400;

                        Player_lol.y = 550;

                        this.ingame_container.addChild( Player_lol );

                        _player = new Player();

                        this.ingame_container.addChild( _player );

                        _player.x = 400;

                        _player.y = 550;

                }

                private function add_mobs(){

                        _enemies = [];

                        for(var e = 0; e <= 19; e++){ ;

                        var _enemy = new Enemy  ;

                        var ex = (e % 5)*100 + 200;

                        var ey = Math.floor(e / 5) * 50 + 100;

                        this.ingame_container.addChild( _enemy );

                        var obj = {link:_enemy,free:false,x:ex,y:ey};

                        _enemies.push( obj );

                        _enemy.x = ex;

                        _enemy.y = ey;

                }

        }

        private function in_game_click( e:MouseEvent ){

                if (! initialized){return;}

                if (cproj){return;}

                _proj = new Projectile  ;

                this.ingame_container.addChild( _proj );

                _proj.x = _player.x;

                _proj.y = _player.y;

                cproj = true;

                BlowID =setInterval (_proj_blow, 40);

        }

        private function _proj_blow(){

                var a = new Effect_blow();

                a.x = _proj.x;

                a.y = _proj.y;

                ingame_container.addChild(a);

        }

}

}


Поделиться:

Компас своими руками

Акварельный мастер-класс "Прощание с детством"

Дымковский петушок

Три коробки с орехами

Космический телескоп Хаббл изучает загадочную "тень летучей мыши"