This site requires JavaScript, please enable it in your browser!
Greenfoot back
Vannaem96
Vannaem96 wrote ...

2016/10/31

Java help

Vannaem96 Vannaem96

2016/10/31

#
So i'm pretty new to Java coding so maybe someone can help. I have to make a program where there are rabbits and wolves, where the wolves eat the rabbits so I', trying to figure out how to make the rabbit disappear when it collides into the wolf. var skyBlue = color(100,200,255); var pumpkin = color(255, 117, 24); var periwinkle = color(204, 204, 255); var deepRed = color(200,0,0); stroke(pumpkin); var stepCount = 0; var xlist = ; var ylist = ; var speedX = ; var speedY = ; var collision = ; var i = 0; while (i<15) { xlist.push(random(0, 500)); ylist.push(random(0, 500)); speedX.push(random(-1, 1)); speedY.push(random(-1, 1)); collision.push(0); i++; } var ballSize = 20; var redDuration = 15; //15 frames is about 1/4 sec. strokeWeight(3); draw = function () { stepCount++; background(periwinkle); //Check to see if any balls collide. var i = 0; while (i<xlist.length) { var k = i+5; //Why is k started at i+1? while (k<xlist.length) { if (isCollision(i,k)) break; k++; } i++; } //Each ball that did not hit another ball gets moved in a random direction //All balls are rendered. i = 0; while (i<xlist.length) { xlist = xlist + speedX; ylist = ylist + speedY; if (xlist + ballSize/2 > canvasWidth) speedX = random(-5, 1); if (ylist + ballSize/2 > canvasHeight) speedY = random(-5, 1); if (xlist - ballSize/2 < 0) speedX = random(1, 5); if (ylist - ballSize/2 < 0) speedY = random(1, 5); if (collision > 0) { collision--; fill(deepRed); } else fill(skyBlue); ellipse(xlist, ylist, ballSize, ballSize); i++; } }; isCollision = function(i,k) { var deltaX = xlist - xlist; var deltaY = ylist - ylist; var distanceBetweenCenters = Math.sqrt(deltaX * deltaX + deltaY * deltaY); if (distanceBetweenCenters <= ballSize) { if (xlist < xlist) { speedX = random(-5, -1); speedX = random(1, 5); } else { speedX = random(1, 5); speedX = random(-5, -1); } if (ylist < ylist) { speedY = random(-5, -1); speedY = random(1, 5); } else { speedY = random(1, 5); speedY = random(-5, -1); } collision = redDuration; collision = redDuration; return true } return false; };
Super_Hippo Super_Hippo

2016/10/31

#
I doubt that you posted the correct code or at least I am not sure how this is related to your question. Well, you could have something like this in your wolf class:
public void act()
{
    //movement...
    
    if (isTouching(Rabbit.class))
    {
        removeTouching(Rabbit.class);
    }
}
You need to login to post a reply.