Sign in to follow this  
aafran

Make player lose points

Recommended Posts

Hello! I am making my first ever game and am having trouble getting my character to lose points. 

In the game, the character can collect two types of bottles. One of them rewards them points, while the other one I want to take away points. However, after doing the codes, my character is still getting points from collecting the wrong bottles (the ones that should take points away)

This was the code I am using to take points from the player (it is in the update function)

    function collectbottle (player, bottle) {

    // Removes the bottle from the screen
    bottle.kill();

    //  Subtract and update the score
    score -= score -1;
    scoreText.text = 'score: '-score;

}

Anyone have tips or ideas of how to fix? :) Thank you!

.png

Share this post


Link to post
Share on other sites

Not sure exactly how you are creating the two separate bottles. Basically what you could do is create two separate variables to store the two separate bottles such as

 

// Create the bottle that you want to increase the score 

this.bottleOne = this.game.add.sprite(x, y, 'bottleOne');

 

// Create the second bottle that you wish to decrease the player score

 

this.bottleTwo = this.game.add.sprite(x, y, 'bottleTwo');

 

//In update section create two separate collisions

 

update() {

//Create the collision in update loop for bottleOne that will increase score and call the bottleOneCollide function

this.game.physics.arcade.collide(this.player, this.bottleOne, this.bottleOneCollide, null, this);

//Create the collision in update loop for bottleTwo that will increase score and call the bottleTwoCollide function

this.game.physics.arcade.collide(this.player, this.bottleTwo, this.bottleTwocollide, null, this);

}

//Create a function to handle collision between player and bottleOne

bottleOneCollide() {

score += 1

this.bottleOne.kill();

}

//Create a function to handle the collision between the player and bottleTwo

bottleTwoCollide() {

score -= 1

this.bottleTwo.kill();

}

 

This is all assuming that you are using arcade physics with your game. The way I have written it here is as if you are using ES6 since that is what I am used to working in but if you are not it would only take a couple small tweaks to get it to work. Hope that this helps out. 

Share this post


Link to post
Share on other sites
On 10/17/2016 at 2:25 AM, tips4design said:

Show the code where you add the collision event callbacks.

Do correct me if I'm wrong and showing the wrong code (like I said, I'm quite new to most of this), but this is my water and bottle collision commands in he create function (both 'water' and 'bottle' sprites are duplicated at least 15 times, hence the water'1' and bottle'1' addition)

game.physics.arcade.collide(player, water1, null, onCollision, this);

game.physics.arcade.collide(player, bottle1, null, onCollision, this); 

 

 


 

Share this post


Link to post
Share on other sites
//  Subtract and update the score
score -= score -1;
scoreText.text = 'score: '-score;

This, I think, is the crux of your problem.

score = score - 1
scoreText.text = 'Score: ' + score

Your original code subtracts and makes equal (-=) the variable score with the outcome of `score - 1`, e.g.

// Pseudo-code
// where score === 10
score = score - (score - 1)
// e.g. score = 10 - (10 - 1)
// therefore, score = 1, after the operation

Whereas:

// Pseudo-code
// where score === 10
score = score - 1
// e.g. score = 10 - 1
// therefore, score = 9

The same can be achieved with:

score -= 1

Up to you which you decide is more readable and more manageable, as a very general rule of thumb the first (score = score - 1) is more explicit and evades potential coding errors (such as the error in your original code).

Regarding the last line where you assign scoreText, you've either just made a typo or don't understand how type coercion works in JS (its not a big criticism, type coercion is an odd and mystical thing in JS)

When you apply an integer to a string, as in `"Score:" + 10`, JS will coerce 10 into its string form "10" and then concatenate the two strings (strings are often immutable so a new string will be created, it doesn't matter for you in JS as you have no power of any of that stuff.

You can not apply the - operator to strings, e.g. "test" - "t" or "test - 10 will always result in NaN, which, in JS, refers to NotANumber, i.e. explosion. This is because where - (substitution) is concerned JS will try to coerce your strings into numbers (either integers or doubles) and it can not coerce "test" into any number, hence, the operation becomes `NaN - 10` and performing most operations on NaN will also produce NaN. Where coercion of strings can occur, it will, i.e. "10" - 1 will work as JS can safely coerce the string "10" into the number 10.

But, you don't need to perform a subtraction on the strings, your variable `score` is your point of truth for your player score, the text is simply a visual representation of the score integer variable and JS allows you to concatenate strings (via the + operator) rather than perform more complex substitution based string manipulation (which you can also do), additionally, the latest JS spec simplifies string substitution via template strings, but, you can safely ignore all that and just `var text = "Some text: " + 100, for example.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.