我怎样才能让这个自上而下的射击游戏运行起来?

时间:2015-11-18 05:04:15

标签: javascript html html5

今天花了6个小时从一个教程修改它为我的元素,我已经尝试了几个小时让它工作,我只是不能:/

以下是我正在遵循的教程的链接: http://gamedevelopment.tutsplus.com/tutorials/create-a-simple-space-shooter-game-in-html5-with-easeljs--active-10944

包含GIF等所有文件的Zip:

http://www.filedropper.com/shooter_1



Raw Javascript Code:

/* Variable Declarations */
var Lives = new Container(); //stores the lives gfx 
var Bullets = new Container(); //stores the bullets gfx 
var Enemies = new Container(); //stores the enemies gfx 
var Boss_Health = 20; 
var Score; 
var Gfx_Loaded = 0; //used as a preloader, counts the already loaded items 
var Center_X = 960; 
var Center_Y = 540; 
var Ticker_Listener = new Object(); //used as a Ticker listener 
var Timer_Source; //references a setInterval method

/* Define Canvas*/
var canvas;
var stage;

/* Background */
var BG_Img = new Image();
var BG;
var BG2_Img = new Image();
var BG2;

/* Characters */
var Player1_Img = new Image();
var Player1;

/* Enemies */
 var Enemy1_Img = new Image();

/* Boss */
var Boss1_Img = new Image();
var Boss1;

/* Lives */
var Life_Img = new Image();

/* Bullets */
var Bullet_Img1 = new Image();

/* Alerts */
var Win_Img = new Image ();
var Lose_Img = new Image ();
var Win;
var Lose;



/*Initiation Function*/
function Main () {
canvas = document.getElementbyId('Shooter');
stage = new stage(canvas);
stage.mouseEventsEnabled = true;

/*Sounds*/

SoundJS.addBatch([ 
        {name:'Boss', src:'Boss.mp3', instances:1}, 
        {name:'Shuriken_Hit', src:'Shuriken_Hit.mp3', instances:10}, 
        {name:'Kunai_Throw', src:'Kunai_Throw.mp3', instances:10}]);

		
/* Load GFX */
BG_Img.src = 'BG_Img.jpg'; 
BG_Img.name = 'BG'; 
BG_Img.onload = loadGfx; 
      
BG2_Img.src = 'BG2_Img.jpg'; 
BG2_Img.name = 'BG2'; 
BG2_Img.onload = loadGfx; 
      
Player1_Img.src = 'Naruto_Idle.gif'; 
Player1_Img.name = 'Player1'; 
Player1_Img.onload = loadGfx; 
  
Enemy1_Img.src = 'Basic_Enemy.gif'; 
Enemy1_Img.name = 'Enemy1'; 
Enemy1_Img.onload = loadGfx; 
  
Boss1_Img.src = 'Akatsuki_Boss.gif'; 
Boss1_Img.name = 'Boss1'; 
Boss1_Img.onload = loadGfx; 
  
Life_Img.src = 'Life.gif'; 
Life_Img.name = 'Life'; 
Life_Img.onload = loadGfx; 
  
Bullet_Img1.src = 'Rasengan_Mid_Air.gif'; 
Bullet_Img1.name = 'Bullet'; 
Bullet_Img1.onload = loadGfx; 
  
Win_Img.src = 'Game_Win.gif'; 
Win_Img.name = 'Game_Win'; 
Win_Img.onload = loadGfx; 
  
Lose_Img.src = 'Game_Over.gif'; 
Lose_Img.name = 'Game_Over'; 
Lose_Img.onload = loadGfx;

/* Ticker */
  
Ticker.setFPS(30); 
Ticker.addListener(stage);


}



/*Preload Function */

function loadGfx(e) 
{ 
    if(e.target.name = 'BG'){BG = new Bitmap(BG_Img);} 
    if(e.target.name = 'BG2'){BG2 = new Bitmap(BG2_Img);} 
    if(e.target.name = 'Player1'){Player1 = new Bitmap(Player1_Img);} 
      
    gfxLoaded++; 
      
    if(gfxLoaded == 9) 
    { 
        addGameView(); 
    } 
}

/* Game View Function */

function addGameView() 
{ 
    Player1.x = Center_X - 18.5; 
    Player1.y = 480 + 34; 
      
    /* Add Lives */
      
    for(var i = 0; i < 3; i++) 
    { 
        var l = new Bitmap(Life_Img); 
          
        l.x = 248 + (25 * i); 
        l.y = 463; 
          
        Lives.addChild(l); 
        stage.update(); 
    } 
      
    /* Score Text */
      
    Score = new Text('0', 'bold 14px Courier New', '#FFFFFF'); 
    Score.maxWidth = 1000;  //fix for Chrome 17 
    Score.x = 2; 
    Score.y = 476; 
      
    /* Second Background */
      
    BG2.y = -480; 
      
    /* Add gfx to stage and Tween Ship */
      
    stage.addChild(BG, BG2, Player1, Enemies, Bullets, Lives, Score); 
    Tween.get(Player1).to({y:425}, 1000).call(Start_Game); 
}

/*Move the Player*/

function Move_Player1(e) 
{ 
    Player1.x = e.stageX - 18.5; 
}

/* Shooting */

function Shoot() 
{ 
    var b = new Bitmap(Bullet_Img1); 
      
    b.x = Player1.x + 13; 
    b.y = Player1.y - 20; 
      
    Bullets.addChild(b); 
    stage.update(); 
      
    SoundJS.play('Kunai_Throw'); 
}

/* Adding Enemies */

function Add_Enemy ()
{
	var e = new Bitmap(Enemy1_Img);
	
	e.x = Math.floor(Math.random() * (320-50))
	e.y = -50
	
	Enemies.addChild(e);
	stage.update();
}

function Start_Game() 
{ 
    stage.onMouseMove = Move_Player1; 
    BG.onPress = Shoot; 
    BG2.onPress = Shoot; 
      
    Ticker.addListener(Ticker_Listener, false); 
    Ticker_Listener.tick = update; 
    Timer_Source = setInterval('addEnemy()', 1000); 
}

function update() 
{ 
/* Move Background */
      
    BG.y += 5; 
    BG2.y += 5; 
      
    if(BG.y >= 480) 
    { 
        BG.y = -480; 
    } 
    else if(BG2.y >= 480) 
    { 
        BG2.y = -480; 
    }

/* Move Bullets */
      
    for(var i = 0; i < Bullets.children.length; i++) 
    { 
        Bullets.children[i].y -= 10; 
    }
	
/* Clear offstage Bullets */

if(Bullets.children[i].y < - 20) 
        { 
            Bullets.removeChildAt(i); 
        } 
    }
	
/* Show Boss */
      
    if(parseInt(Score.text) >= 1000 && Boss1 == null) 
    { 
        Boss1 = new Bitmap(Boss1_Img); 
                  
        SoundJS.play('Boss'); 
                  
        Boss1.x = Center_X - 90; 
        Boss1.y = -183; 
                  
        stage.addChild(boss1); 
        Tween.get(boss1).to({y:40}, 2000)   //tween the boss onto the play area 
    }
	
/* Move Enemies */
	
	 for(var j = 0; j < Enemies.children.length; j++) 
    { 
        eEemies.children[j].y += 5; 
          
        /* Remove Offstage Enemies */
          
        if(Enemies.children[j].y > 480 + 50) 
        { 
            Enemies.removeChildAt(j); 
        }
	}
		
/* Bullet - Enemy Collision */

for(var k = 0; k < Bullets.children.length; k++) 
{       
    if(Bullets.children[k].x >= Enemies.children[j].x && Bullets.children[k].x + 11 < Enemies.children[j].x + 49 && Bullets.children[k].y < Enemies.children[j].y + 40) 
    { 
        Bullets.removeChildAt(k); 
        Enemies.removeChildAt(j); 
        stage.update(); 
        SoundJS.play('Shuriken_Hit'); 
        Score.text = parseFloat(Score.text + 50); 
    }
	
/* Bullet - Boss Collision */
              
    if(Boss1 != null && bullets.children[k].x >= Boss1.x && Bullets.children[k].x + 11 < Boss1.x + 183 && Bullets.children[k].y < Boss1.y + 162) 
    { 
        Bullets.removeChildAt(k); 
        Boss_Health--; 
        stage.update(); 
        SoundJS.play('Shuriken_Hit'); 
        Score.text = parseInt(Score.text + 50); 
    } 
}

/* Player1 - Enemy Collision */
          
    if(Enemies.hitTest(Player1.x, Player1.y) || Enemies.hitTest(Player1.x + 37, Player1.y)) 
    { 
        Enemies.removeChildAt(j); 
        Lives.removeChildAt(Lives.length); 
        Player1.y = 480 + 34; 
        Tween.get(Player1).to({y:425}, 500) 
        SoundJS.play('Shuriken_Hit'); 
    } 
	
/* Check for win */
      
    if(Boss1 != null && Boss_Health <= 0) 
    { 
        alert('Win'); 
    } 
      
    /* Check for lose */
      
    if(Lives.children.length <= 0) 
    { 
        alert('Lose'); 
    } 

function alert(e) 
{ 
    /* Remove Listeners */
          
    stage.onMouseMove = null; 
    BG.onPress = null; 
    BG2.onPress = null; 
      
    Ticker.removeListener(Ticker_Listener); 
    Ticker_Listener = null; 
      
    Timer_Source = null; 
      
    /* Display Correct Message */
      
    if(e == 'Win') 
    { 
        Win = new Bitmap(Win_Img); 
        Win.x = centerX - 64; 
        Win.y = centerY - 23; 
        stage.addChild(Win); 
        stage.removeChild(Enemies, Boss1); 
    } 
    else
    { 
        Lose = new Bitmap(Lose_Img); 
        Lose.x = centerX - 64; 
        Lose.y = centerY - 23; 
        stage.addChild(Lose); 
        stage.removeChild(Enemies, Player1); 
    } 
      
    BG.onPress = function(){window.location.reload();}; 
    BG2.onPress = function(){window.location.reload();}; 
    stage.update(); 
}
&#13;
Raw HTML Code:
<!doctype html>
<html>
	<head>
		<title>Scrolling Shooter</title>
		<meta charset="utf-8">
		<style>*{-webkit-tap-highlight-color: rgba(0, 0, 0, 0);}</style> 
      		<link rel="stylesheet" href="Week11.css">
			
		<script src="https://code.createjs.com/easeljs-0.8.1.min.js"></script>
		<script src="https://code.createjs.com/tweenjs-0.6.1.min.js"></script>
		<script src="https://code.createjs.com/soundjs-0.6.1.min.js"></script>
		
	</head>
	
	
	
	
	<body onload="Main();">
		<div id="stage">
			<canvas id= "Shooter"> width="1920" height="1080" </canvas>
		</div>
		
		<script src="Main.js"> </script>
		
	</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您正在使用的教程非常过时(它使用的是3年或4年的CreateJS版本)。您在样本中使用的EaselJS版本于2015年5月发布。

首先,所有CreateJS类(例如Container)都需要使用createjs命名空间:

Player1 = new createjs.Bitmap(Player1_Img);
Score = new createjs.Text('0', 'bold 14px Courier New', '#FFFFFF');

Ticker API也发生了变化:

createjs.Ticker.addEventListener("tick", stage);
// OR
createjs.Ticker.on("tick", stage);

还有其他API已更改(例如BitmapAnimation变为Sprite)。

我会从那里开始,然后在你取得一些进展后跟进。使用您的控制台确定发生了什么错误,并一次解决一个错误。

干杯,