Priority in Sprite Collisions - Please Help this Student!

csd-unit-3-lesson-22

#1

I have a student who is developing a game for the lesson 22 project and is having some difficulty with sprite interactions. The game is a platform with two aliens (sprites) who are attempting to push each other off the edge. They are controlled with separate keys. The problem he is having is that one sprite appears to take priority over the other and “always wins” by pushing the other off the edge. Is there a way to correct/improve this? I could not seem to figure out how to do it in class so I am sending it out to the community with the hope someone knows how to solve this one. Please help! Here is the current program as a reference:
// Create your variables here
var lives1 = 3;
var lives2 = 3;
// Create your sprites here
var stage = createSprite(200, 370);
stage.setAnimation(“stage”);
stage.scale = 0.87;
var player1 = createSprite(75, 280);
player1.setAnimation(“player1”);
player1.scale = 0.70;
var player2 = createSprite(325, 280);
player2.setAnimation(“player2”);
player2.scale = 0.70;
var win1 = createSprite(200, 200);
win1.setAnimation(“oneWin”);
win1.scale = 3.3;
win1.visible = 0;
var win2 = createSprite(200, 200);
win2.setAnimation(“twoWin”);
win2.scale = 3.3;
win2.visible = 0;
function draw() {
// draw background
backgrounds();
scoreDisplay();
// update sprites
gravityCollide();
playerMovement1();
playerMovement2();
collision();
offstage();
gameover();
drawSprites();
}
// Create your functions here
function backgrounds() {
background(rgb(58, 130, 244));
if (lives1 == 1) {
background(rgb(188, 68, 32));
}
if (lives2 == 1) {
background(rgb(188, 68, 32));
}
}
function scoreDisplay() {
fill(“black”);
text(“P1 Lives:”, 40, 20);
text(lives1, 60, 40);
text(“P2 Lives:”, 300, 20);
text(lives2, 320, 40);
}
function gravityCollide() {
player1.collide(stage);
player1.velocityY = player1.velocityY + 0.2;
player2.collide(stage);
player2.velocityY = player2.velocityY + 0.2;
}
function playerMovement1() {
if (keyWentDown(“w”)) {
player1.velocityY = -5;
}
if (keyDown(“a”)) {
player1.x = player1.x - 4.5;
}
if (keyDown(“d”)) {
player1.x = player1.x + 4.5;
}
if (keyDown(“s”)) {
player1.x = player1.x + 15;
}
}
function playerMovement2() {
if (keyWentDown(“up”)) {
player2.velocityY = -5;
}
if (keyDown(“left”)) {
player2.x = player2.x - 4.5;
}
if (keyDown(“right”)) {
player2.x = player2.x + 4.5;
}
if (keyDown(“down”)) {
player2.x = player2.x + -15;
}
}
function collision() {
player1.bounce(player2);
}
function offstage() {
if (player1.x < -10) {
lives1 = lives1 - 1;
player1.x = 100;
player1.y = 130;
player1.velocityX = 0;
}
if (player1.x > 410) {
lives1 = lives1 - 1;
player1.x = 100;
player1.y = 130;
player1.velocityX = 0;
}
if (player2.x < -10) {
lives2 = lives2 - 1;
player2.x = 300;
player2.y = 130;
player2.velocityX = 0;
}
if (player2.x > 410) {
lives2 = lives2 - 1;
player2.x = 300;
player2.y = 130;
player2.velocityX = 0;
}
}
function gameover() {
if (lives1 == 0) {
win2.visible = 1;
}
if (lives2 == 0) {
win1.visible = 1;
}
}


#2

Can you please post a link to the project? It makes looking at the code a lot easier


#3

#4

Piercec,

So from the game to shared, both aliens will bounce off of each other and continue moving backwards until they fall off the edge. I would look at the collision the student used and look at the other options (collide, displace, bounce, bounceOff) from Lesson 18 to see which works the best.

Update: As a finished typing all that - the remixed code is different from the copy and pasted code above - did the student figure it out? Both of the aliens continue to move backward off the stage, but I don’t know what the original code was to see if this version is any better.

Brad