Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Mmarzex

Hi,

I just started learning Impact today and I was trying to figure out how I could add scoring to the pong example. How would I go about making it that some variable score is incremented and printed whenever the puck hits the goal?

1 decade ago by Joncom

I suspect you will want to maintain the score in your main.js file. Can we see what you have so far?

1 decade ago by Mmarzex

I guess What I don't understand right now is how to I go about keeping the score maintained? Would I use a class variable? Or would I need to somehow update the score whenever one side scores and then start a new game but with the new score intact?

1 decade ago by mLautz

I haven't actually looked at the pong example, but if the game were simple enough I would just maintain it as a variable in the main game. Increment it when necessary and then have part of your draw function write text with the value of your variable to the screen. This will pick up any change in the score as soon as the screen is redrawn.

I'm still picking up Impact, so let me know if there is a better way. You could always add the same logic to an entity as well.

Example:
MyGame = ig.Game.extend({
        // Load a font
        statText: new ig.Font('media/chuckthecon/04b03.font.png'),
        // Create score variable
        currScore: 0,

.
.
.
.

draw: function() {
                this.parent();
                //draw the score somewhere in the top right corner
                this.statText.draw(this.currScore, ig.system.width - 50, 10, ig.Font.ALIGN.RIGHT);
}

1 decade ago by Mmarzex

Yeah that makes sense. My wondering now is what collision tile I would use to represent the puck going in the goal and then how to actually take that collision and make it increment and then restart the puck in the middle. Time to start messing around with it.

1 decade ago by mLautz

Not sure if there is a better approach, but one way you could do this would be to create an entity that you place at the back of each goal. Have the entity check against the puck, and if a collision is detected destroy the puck, increment the score, and call some function that starts the next round.

check: function(other){
        if(other instanceof EntityPuck){
              other.kill();
              //increment score
              //puck reset function
        }
}

1 decade ago by Mmarzex

Alright so I have added a kill entity that I put at the end of the field and it works that it kills the puck but then freezes. in my kill.js entity file I put this code

checkAgainst: ig.Entity.TYPE.A,
check: function( other ) {

        other.kill();
        this.gameOver = true;
}

Now in my update function in main.js I put this

if( ig.input.pressed('ok')) {
            ig.system.setGame( main );
        }

        if( this.gameOver ) {
            return;
        }

if (kill.gameOver)
            this.gameOver = true;

I thought this would work to reset it but it doesn't and just freezes on load with all of that. I don't even know where to start with the score in this case.

1 decade ago by mLautz

Have you checked your browser console for error messages? It may be failing to load the game.

Is there a specific reason you want to reload the entire game? If you are only doing so to reset the puck, you should look into .spawnEntity() of the Game class. This would allow you to create a new puck without reloading the whole level, and then you could also maintain a score variable within your game class (that does not reset when you load the game again).


Example, to reload the puck at the center of the screen:
ig.game.spawnEntity(EnityPuck, ig.system.width/2, ig.system.height/2, {});

Another suggestion would be to simply reuse the puck instead of killing it, if this fits your needs. To do that you would simply reset the .pos.x and .pos.y of your puck when you would normally kill it.

1 decade ago by Mmarzex

The spawning a new One makes more sense, How exactly would I call the spawnEntity and change the score with in my main.js Game file. Or would I do that within the kill entity or puck entity?

1 decade ago by mLautz

If in your main.js file you have the something like this...
.defines(function(){
    MyGame = ig.Game.extend({
            //Set score variable
            score: 0,

Then you could reference that variable from other locations (such as the puck killing entity) like so:
ig.game.score++;

You could also write a function in your main to handle this for you:
playerScored: function(){
    this.score++;
    //Spawn a new puck at the center of the screen with x vel of 60
    ig.game.spawnEntity(EnityPuck, ig.system.width/2, ig.system.height/2, {vel.x: 60});
}

and call that instead from your puck killing entity
ig.game.playerScored();

All of this is written assuming you are only tracking the score for one player. If you wanted to track the score for both sides, you would simply need two separate variables and functions to call.

1 decade ago by Mmarzex

That works, thanks so much. I am now on to making the cpu paddle actually beatable. That is just having to find something to change up how it moves right?

1 decade ago by mLautz

I have not yet tried to write flaws into an AI yet but, like you said, it should be a matter of changing the movement logic to allow for openings in the goal protection.

1 decade ago by Mmarzex

Yeah I figured out to just flaw it slightly so that there is a small opening but still makes it a hard game.
Page 1 of 1
« first « previous next › last »