2013-03-07 2 views
1

스프라이트 시트의 특정 지점에서 그려지는 값이 포함 된 명령문을 임의로 선택하려고합니다. 이것이 현재 코드입니다.자바 스크립트 html5 캔버스 랜덤 스위치 명령문 이미지

this.asteroid = Math.floor(Math.random()*7+1); 
    switch(this.asteroid) 
    { 
    case 0: 
     this.srcX = 0; 
     this.srcY = 528; 
     this.width = 32; 
     this.height = 33; 
     break; 
    case 1: 
     this.srcX = 32; 
     this.srcY = 528; 
     this.width = 32; 
     this.height = 33; 
     break; 
    case 2: 
     this.srcX = 64; 
     this.srcY = 528; 
     this.width = 32; 
     this.height = 33; 
     break; 
    case 3: 
     this.srcX = 63; 
     this.srcY = 565; 
     this.width = 62; 
     this.height = 60;   
     break; 
    case 4: 
     this.srcX = 125; 
     this.srcY = 565; 
     this.width = 62; 
     this.height = 60;   
     break; 
    case 5: 
     this.srcX = 187; 
     this.srcY = 565; 
     this.width = 62; 
     this.height = 60;   
     break; 
    case 6: 
     this.srcX = 0; 
     this.srcY = 632; 
     this.width = 116; 
     this.height = 120;   
     break; 
    } 

나중에 선택한 값을 그립니다.

문제는 대부분 그림이 그려지지만 동시에 빈 이미지 만 그려야합니다. 모든 X 및 Y 위치를 확인했으며 스프라이트 시트에서 모두 정확하고 일치합니다.

this.drawX -= this.speed; 
ctxEnemy.drawImage(imgSprite, 
    this.srcX+this.width,this.srcY,this.width,this.he‌​ight, 
    this.drawX,this.drawY,this.width,this.height); 
this.checkEscaped(); 
+0

예제 코드를 jsFiddle에 넣거나 적어도 그리기 작업을 수행하는 코드를 질문에 추가 할 수 있습니까? – BinaryTox1n

+0

'this.drawX - = this.speed; ' \t'ctxEnemy.drawImage (imgSprite, this.srcX + this.width, this.srcY, this.width, this.height, this.drawX, this.drawY, this .width, this.height); ' 'this.checkEscaped(); ' 코드를 그립니다. – Nick

답변

2

Math.floor(Math.random()*7+1)+1을 제거 7. 1에서 값을 사용할 수 있습니다 아래

내가 내 스프라이트를 그릴 사용하고있는 코드입니다.

+0

+1이 없으면 7 개의 이미지 중 5 개만 그립니다. – Nick

+0

그런 다음 다른 문제가 있습니다. 첫 번째 스위치를 포함하여 스위치의 모든 분기를 탐색하려는 경우 +1은 버그입니다. –

+0

@drystroy 죄송합니다 정확히 무엇을 의미합니까? – Nick

0

난 당신이 드로잉 코드에 문제가있을 수 있습니다 생각 : 라인에서

this.drawX -= this.speed; 
ctxEnemy.drawImage(imgSprite, 
    this.srcX+this.width,this.srcY,this.width,this.he‌​ight, 
    this.drawX,this.drawY,this.width,this.height); 
this.checkEscaped(); 

: 당신은 개체를 추가 this.srcX+this.width,this.srcY,this.width,this.he‌​ight, 소스 위치로 폭. 나는 이것이 당신의 스프라이트 그림을 던질 수 있다고 생각합니다. 아래의 참조 주석이있는 예제를 참조하십시오.

// draw image to screen drawImage(imageObject, 
//  sourceX, sourceY, sourceWidth, sourceHeight, 
//  destinationX, destinationY, destinationWidth, destinationHeight) 
ctx.drawImage(imageObject, 
    this.srcX, this.srcY, this.width, this.height, 
    this.drawX, this.drawY, this.width, this.height); 
관련 문제