日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
蘑菇與熊游戲開(kāi)發(fā)第四回(熊碰撞邊界處理)

第四回主要講熊移動(dòng)碰到邊界時(shí)的反彈處理

預(yù)期達(dá)到效果:http://www.html5china.com/html5games/mogu/index3.html

一、寫一個(gè)碰撞處理函數(shù)

 
 
 
  1. function HasAnimalHitEdge()      
  2. {      
  3.     //熊碰到右邊邊界      
  4.     if(animal.x>screenWidth - animal.image.width)      
  5.     {      
  6.         if(horizontalSpeed > 0)//假如向右移動(dòng)      
  7.             horizontalSpeed =-horizontalSpeed;//改變水平速度方向      
  8.     }      
  9.     //熊碰到左邊邊界      
  10.     if(animal.x<-10)      
  11.     {      
  12.         if(horizontalSpeed < 0)//假如向左移動(dòng)      
  13.             horizontalSpeed = -horizontalSpeed;//改變水平速度方向      
  14.     }      
  15.     //熊碰到下面邊界      
  16.     if(animal.y>screenHeight - animal.image.height)      
  17.     {      
  18.         //2秒鐘后從新開(kāi)始      
  19.         setTimeout(function(){      
  20.             horizontalSpeed = speed;      
  21.             verticalSpeed = -speed;      
  22.             animal.x = parseInt(screenWidth/2);      
  23.             animal.y = parseInt(screenHeight/2);      
  24.             gameLoop();      
  25.         }, 2000);      
  26.     }      
  27.     //熊碰到上邊邊界      
  28.     if(animal.y<0)      
  29.     {      
  30.         verticalSpeed = -verticalSpeed;      
  31.     }      
  32. }    

二、在游戲循環(huán)GameLoop()尾部中加入檢測(cè)邊界函數(shù),如下

 
 
 
  1.   function GameLoop()         
  2.   {         
  3.       //清除屏幕         
  4.       ctx.clearRect(0, 0, screenWidth, screenHeight);         
  5.       ctx.save();         
  6.       //繪制背景         
  7.       ctx.drawImage(backgroundForestImg, 0, 0);         
  8.       //繪制蘑菇         
  9.       ctx.drawImage(mushroom.image, mushroom.x, mushroom.y);       
  10. //繪制熊      
  11. //改變移動(dòng)動(dòng)物X和Y位置      
  12. animal.x += horizontalSpeed;      
  13. animal.y += verticalSpeed;      
  14. //改變翻滾角度      
  15. animal.angle += bearAngle;      
  16. //以當(dāng)前熊的中心位置為基準(zhǔn)      
  17.         ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));      
  18. //根據(jù)當(dāng)前熊的角度輪換      
  19.     ctx.rotate(animal.angle * Math.PI/180);      
  20. //描繪熊      
  21.     ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));      
  22.       ctx.restore();      
  23. //檢測(cè)是否碰到邊界      
  24. HasAnimalHitEdge();      
  25.       }       

到此第四回的完整代碼如下:

 
 
 
  1.         
  2.         
  3.         
  4.         
  5. 蘑菇動(dòng)起來(lái)-html5中文網(wǎng)        
  6.      
  7.         
  8.         
  9.         
  10.         
  11.         
  12.             
  13.                
  14.         瀏覽器不支持html5,請(qǐng)下載支持html5的瀏覽器來(lái)觀看       
  15.                 
  16.     
        
  •                
  •        
  • 第四回就講到這了,第五回講熊碰撞蘑菇的事件

    【編輯推薦】

    1. 蘑菇與熊游戲開(kāi)發(fā)***回(游戲分析)
    2. 蘑菇與熊游戲開(kāi)發(fā)第二回(讓蘑菇動(dòng)起來(lái))
    3. 蘑菇與熊游戲開(kāi)發(fā)第三回(讓熊動(dòng)起來(lái))
    4. 蘑菇與熊游戲開(kāi)發(fā)第五回(熊碰撞蘑菇處理)
    5. 蘑菇與熊游戲開(kāi)發(fā)第六回(繪制獎(jiǎng)品)
    6. 蘑菇與熊游戲開(kāi)發(fā)第七回(熊碰到獎(jiǎng)品處理)
    7. 蘑菇與熊游戲開(kāi)發(fā)第八回(完善游戲)

    分享題目:蘑菇與熊游戲開(kāi)發(fā)第四回(熊碰撞邊界處理)
    文章出自:http://www.dlmjj.cn/article/dhcgpgj.html