高中项目的javascript基本碰撞功能

时间:2018-12-11 18:34:10

标签: javascript canvas

大家好,我正在尝试实现碰撞功能,因此,如果猴子碰到香蕉,香蕉就会移动到画布周围的某个地方。

目前,猴子和椰子在四处移动,猴子和香蕉相撞,但是香蕉之后没有移动。

这是功能:

var mx = canvas.width / 2;     
var my = canvas.height / 2;
var mdx = 0;        
var mdy = 0;
var mspeed = 2;        
var mlives = 3; 
var cx = 400;
var cy = 250;
var cspeed = 3;
var cdx = cspeed - 1;
var cdy = cspeed;
var bx = Math.floor(Math.random() * canvas.width);
var by = Math.floor(Math.random() * canvas.height);
var bananasEaten = 0;
var lives = 3;
var rightPressed = false;
var leftPressed = false;
var upPressed = false;
var downPressed = false;

function getBanana() {
    if (mx <= (bx + bananaImage.width) && bx <= (mx + monkeyImage.width)) {
        if (my <= (by + bananaImage.height) && by <= (my + monkeyImage.height)) {
             bananasEaten = bananasEaten + 1;
             bx = Math.floor(Math.random() * (canvasWidth - bananaImage.width));
             by = Math.floor(Math.random() * (canvasHeight - bananaImage.height));
         }
    }
}

1 个答案:

答案 0 :(得分:0)

因此,基本上,您想检查一下猴子(mx,my)的位置是否与香蕉(bx,by)的位置匹配。因此:

+ mvn test jacoco:report -Dspring.profiles.active=test -Dsurefire.suiteXmlFiles=src/test/resources/unit-tests.xml
Picked up JAVA_TOOL_OPTIONS: -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Dsun.zip.disableMemoryMapping=true
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building myProject-api 1.3.0
[INFO] ------------------------------------------------------------------------
Downloading: https://raw.github.com/genomicsengland/myArtifact/repository/com/my/org/myArtifact/maven-metadata.xml

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.118 s
[INFO] Finished at: 2018-12-11T18:33:18+00:00
[INFO] Final Memory: 29M/922M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project myProject-api: Could not resolve dependencies for project myProject.service:myProject-api:jar:1.3.0: Failed to collect dependencies at com.my.org:myArtifact:jar:[1.0.0,): No versions available for com.my.org:myArtifact:jar:[1.0.0,) within specified range -> [Help 1]

尽管,我应该提一下,您可能需要将此 if( mx + monkey.width >= bx && mx <= bx + banana.width && my + monkey.height >= by && my <= by + banana.height) { // Execute code } 函数放在另一个绘制游戏的函数中,该函数通过getBananasetInterval一遍又一遍地被调用,因为我猜这是一些游戏。