2013-04-18 4 views
1

애니메이션 캐릭터가 화면을 가로 질러 올라가고 위에서 떨어지는 상자를 피하는 이젤 게임 자습서에서 작업 해 왔습니다. 자습서의 코드를 정확히 따라 갔지만 기쁨은 없었습니다. 아무 것도로드되지 않은 것 같습니다 (올바르게 매핑 된 이미지 포함). 구문에 관한 오류는 발생하지 않으므로 문제가 무엇인지 확실하지 않습니다. 그것은 공정한 약간의 코드이므로 아무도 시간을 갖고 있지는 않지만 단지 여기에 해당한다면 완전히 이해할 수 있습니다. 사람이 관심이 있다면HTML5, Easel.js 게임 문제

사이트의 페이지 코드 (병이 개별 자바 스크립트 파일의 코드를 게시, 헤더에 나열된

<!DOCTYPE HTML> 
<html> 
<head> 
<meta charset="utf-8"> 
<style> 
body { 
    background-color: #000; 
} 
</style> 

<script src="lib/easeljs-0.6.0.min.js"></script> 
<script src="JS/Platform.js"></script> 
<script src="JS/Hero.js"></script> 
<script src="JS/Crate.js"></script> 

<script> 
var KEYCODE_SPACE = 32, KEYCODE_LEFT = 37, KEYCODE_RIGHT = 39; 
var canvas, stage, lfheld, rtheld, platforms, crates, hero, heroCenter, key, door, gameTxt; 

var keyDn = false, play=true, dir="right"; 
var loaded = 0, vy = 0, vx = 0; 
var jumping = false, inAir = true, gravity = 2; 

var img = new Image(); 
var bgimg = new Image(); 
var kimg = new Image(); 
var dimg = new Image(); 

var platformW = [300, 100, 180, 260, 260, 100, 100]; 
var platformX = [40, 220, 320, 580, 700, 760, 760]; 
var platformY = [460, 380, 300, 250, 550, 350, 450]; 
document.onkeydown=handleKeyDown; 
document.onkeyup=handleKeyUp; 

function init(){ 
    canvas = document.getElementById("canvas"); 
    stage = new createjs.Stage(canvas); 
    bgimg.onload = this.handleImageLoad; 

    bgimg.src = "img/scene.jpg"; 

kimg.onload = this.handleImageLoad; 

kimg.src="img/key.png"; 

dimg.onload = this.handleImageLoad; 

dimg.src = "img/door.jpg"; 

img.onload = this.handleImageLoad; 

img.src = "img/hero.png"; 
} 

function handleImageLoad(event) { 
    loaded+=1; 
    if (loaded==4){ 
     start(); 
    } 
} 

function handleKeyDown(e) { 
    if(!e){ var e = window.event; } 
    switch(e.keycode) { 
     case KEYCODE_LEFT: lfHeld = true; 
dir="left"; break; 
case KEYCODE_RIGHT: rtHeld = true; 
dir="right"; break; 
case KEYCODE_SPACE: jump(); break; 
    } 
} 


function handleKeyUp(e) { 
    if(!e){ var e = window.event; } 
    switch(e.keycode) { 
     case KEYCODE_LEFT: lfHeld = false; 
keyDn=false; hero.gotoAndStop("idle_h"); break; 
case KEYCODE_RIGHT: rtHeld = false; 
keyDn=false; hero.gotoAndStop("idle"); break; 
    } 
} 




function start(){ 
    var bg = new createjs.Bitmap(bgimg); 
    stage.addChild(bg); 
    door = new createjs.Bitmap(dimg); 
    door.x = 131; 
    door.y = 384; 
    stage.addChild(door); 
    hero = new Hero(img); 
    hero.x = 80; 
    hero.y = 450; 
    stage.addChild(Hero); 

    crates = new Array(); 
    paltforms = new Array(); 
    for(i=0; i < platformW.length; i++){ 
     var platform = new Platform(platformW[i],20); 
     platforms.push(platform); 
     stage.addChild(platform); 
     platform.x = platformX[i]; 
     platform.y = platformY[i]; 
    } 

for(j=0; j < 5; j++){ 
    var crate = new Crate(); 
    crates.push(crate); 
    stage.addChild(crate); 
    resetCrates(crate); 
} 
key = new createjs.Bitmap(kimg); 
key.x = 900; 
key.y = 490; 
stage.addChild(key); 

Ticker.setFPS(30); 
Ticker.addListener(window); 
stage.update(); 
} 

function tick() { 
    heroCenter = hero.y-40; 
    if (play){ 
     vy+=gravity; 
     if (inAir){ 
      hero.y+=vy; 
     } 
     if (vy>15){ 
      vy=15; 
     } 
for(i=0; i < platforms.length; i++){ 
    if (hero.y >= platforms[i].y && hero.y<=(platforms[i].y+platforms[i].height) && hero.x> 
    platforms[i].x && hero.x<(platforms[i]. 
    x+platforms[i].width)){; 
    hero.y=platforms[i].y; 
    vy=0; 
    jumping = false; 
    inAir = false; 
    break; 
    }else{ 
     inAir = true; 
    } 
} 
for(j=0; j < crates.length; j++){ 
    var ct = crates[j]; 
    ct.y+=ct.speed; 
    ct.rotation+=3; 
    if (ct.y>650){ 
     resetCrates(ct); 
    } 
    if (collisionHero (ct.x, ct.y, 20)){ 
     gameOver();} 
} 
if (collisionHero (key.x+20, key.y+20, 
20)){ 
    key.visible=false; 
    door.visible=false; 
} 
if (collisionHero (door.x+20, door.y+40, 
20) && key.visible==false){nextLevel();} 
if (lfHeld){vx = -5;} 
if (rtHeld){vx = 5;} 

if(lfHeld && keyDn==false && inAir==false) 
{ 
    hero.gotoAndPlay("walk_h"); 
    keyDn=true; 
} 
if(rtHeld && keyDn==false && 
inAir==false){ 
    hero.gotoAndPlay("walk"); 
    keyDn=true; 
} 
if (dir=="left" && keyDn==false && 
inAir==false){ 
    hero.gotoAndStop("idle_h"); 
} 
if (dir=="right" && keyDn==false && 
inAir==false){ 
    hero.gotoAndStop("idle"); 
} 

hero.x+=vx; 
vx=vx*0.5; 
if (hero.y>610){ 
    gameOver(); 
} 
    } 
    stage.update(); 
} 

function end(){ 
    play=false; 
    var l = crates.length; 
    for (var i=0; i<l; i++){ 
     var c = crates[i]; 
     resetCrates(c); 
    } 
    hero.visible=false; 
    stage.update(); 
} 

function nextLevel(){ 
    gameTxt = new createjs.Text("Well Done\n\n", 
"36px Arial", "#000"); 
gameTxt.text += "Prepare for Level 2"; 
gameTxt.textAlign = "center"; 
gameTxt.x = canvas.width/2; 
gameTxt.y = canvas.height/4; 
stage.addChild(gameTxt); 
end(); 
} 
function gameOver(){ 
    gameTxt = new createjs.Text("Game Over\n\n", 
    "36px Arial", "#000"); 
    gameTxt.text += "Click to Play Again."; 
    gameTxt.textAlign = "center"; 
    gameTxt.x = canvas.width/2; 
    gameTxt.y = canvas.height/4; 
    stage.addChild(gameTxt); 
    end(); 
    canvas.onclick = handleClick; 
} 

function handleClick() { 
    canvas.onclick = null; 
    hero.visible=true; 
    hero.x = 80; 
    hero.y = 450; 
    door.visible=true; 
    key.visible=true; 
    stage.removeChild(gameTxt); 
    play=true; 
} 
function collisionHero (xPos, yPos, 
Radius){ 
    var distX = xPos - hero.x; 
    var distY = yPos - heroCenter; 
    var distR = Radius + 20; 
    if (distX * distX + distY * distY <= 
distR * distR){ 
    return true; 
} 
} 
function jump(){ 
    if (jumping == false && inAir == false){ 
     if (dir=="right"){ 
      hero.gotoAndStop("jump"); 
     }else{ 
      hero.gotoAndStop("jump_h"); 
     } 
     hero.y -=20; 
     vy = -25; 
     jumping = true; 
     keyDn=false; 
    } 
} 
function resetCrates(crt) { 
    crt.x = canvas.width * Math.random()|0; 
    crt.y = 0 - Math.random()*500; 
    crt.speed = (Math.random()*5)+8; 
} 





</script> 

<title>Game</title> 
</head> 

<body onload="init();"> 




<canvas id="canvas" width="960px" height="600px"></canvas> 



</body> 
</html> 

은 JS 파일을 추가 Platform.js :.

 (function(window) { 
    function Platform(w,h) { 
     this.width = w; 
     this.height = h; 
     this.initialize(); 
    } 

Platform.prototype = new createjs.Container(); 
Platform.prototype.platformBody = null; 
Platform.prototype.Container_initialize = Platform.prototype.initialize; 

Platform.prototype.initialize = function() { 
    this.Container_initialize(); 
    this.platformBody = new createjs.Shape(); 
    this.addChild(this.platformBody); 
    this.makeShape(); 
} 

Platform.prototype.makeShape = function() { 
    var g = this.platformBody.graphics; 
    g.drawRect(0,0,this.width,this.height); 
} 
window.Platform = Platform; 
}(window)); 

영웅 .js

(function(window) { 
    function Hero(imgHero) { 
     this.initialize(imgHero); 
    } 

Hero.prototype = new createjs.BitmapAnimation(); 
Hero.prototype.Animation_initialize = Hero.prototype.initialize; 
Hero.prototype.initialize = function(imgHero) { 
    var spriteSheet = new createjs.SpriteSheet({ 

images: [imgHero], 
frames: {width: 60, height: 85, regX: 29, regY: 80}, animations: { 
    walk: [0, 19, "walk"], 
    idle: [20, 20], 
    jump: [21, 21] } }); 

SpriteSheetUtils 
.addFlippedFrames(spriteSheet, true, false, 
false); 
this.Animation_initialize(spriteSheet); 
this.gotoAndStop("idle"); 
} 
window.Hero = Hero; 
}(window)); 

Crate.js

(function(window) { 
    function Crate() { 
     this.initialize(); 
    } 

Crate.prototype = new createjs.Container(); 
Crate.prototype.img = new Image(); 
Crate.prototype.Container_initialize = 
Crate.prototype.initialize; 
Crate.prototype.initialize = function() { 

this.Container_initialize(); 
var bmp = new createjs.Bitmap("img/crate.jpg"); 
bmp.x=-20; 
bmp.y=-20; 
this.addChild(bmp);} 
window.Crate = Crate; 
}(window)); 
+0

당신이 자습서를 연결하면 도움이 쉬울 것, 나는 오류 오프 손이 보이지 않는, 그러나 나는 또한 이젤 재능이 아니에요. 이 버전의 이전 버전도 있습니까? – baordog

+0

내가 할 수 있으면 좋겠지 만 튜토리얼은 잡지에있다. 나는 아주 새로운 이젤이므로 뭔가 잘못했음을 희망했다. 저의 첫 번째 버전이 나오기 때문에 이전 버전이없는 것 같습니다. init()가 별도의 폴더에 있어야하지만 도움이되지 않는 것 같아요. – user2085143

답변

3

나는 네임 스페이스없이 EaselJS - 객체를 초기화하려고 것으로 나타났습니다 :

stage = new Stage(canvas); 

그러나 0.5.0 이후에는 createjs - 네임 스페이스를 사용 (또는 전에 창에 네임 스페이스를 매핑해야 그래서 당신이 지금해야 할 것이 무엇 모든 easelJS - 클래스가이 같은 새로운 인스턴스를 만들 때 그 전에 createjs.을 추가하는 것입니다) 아무것도 할 : 그 EV 인 경우에

stage = new createjs.Stage(canvas); 

확실하지 않음 erything,하지만 시작, 이것이 도움이되기를 바랍니다.

현재 namepsacing CreateJS에 대한 자세한 내용을보실 수 있습니다 : https://github.com/CreateJS/EaselJS/blob/master/README_CREATEJS_NAMESPACE.txt

+0

시도했지만 여전히 운이 없다! 배경 이미지와 다른 이미지가 아직로드 중이어야하고 라인 다운 오류가있는 경우 아무 것도로드하지 않습니까? – user2085143

+0

또한 입력 된 "createjs"를 표시하고 js 파일을 추가로 편집했습니다 – user2085143

+0

어쩌면 온라인에 모든 것을 넣을 수 있습니까? 어떤 종류의 오류가있을 것입니다 ... 실수로 실행을 볼 때 실수를 확인하는 것이 훨씬 쉽습니다. – olsn