Scoring a game for beginners

Hi All!

I’m new to Hype and I don’t know Java. I want to keep track of the score in the game I’m building, and I’ve watched several tutorials and analyzed several sample games, and I keep getting stuck.

Can you help me?

I’d like to start with a score of 10.

I want the running score to show up on every page (and I keep getting caught by the lack of a unique ID for the element of the score box.)

I’d like to be able to add or subtract points based on what the players do.

Please help a novice!

Thank you!
Beth

1 Like

It's JavaScript, sounds similar but it's a different language.

I wrote A Book About Hype, and one of the main projects covers this topic. Here's a demo...

Well, the first part of the code would be to create a variable. That's fairly straightforward...

 window.score = 10;

I use "window.score" as that's a "Global Variable". More info here... JavaScript Scope ...basically, I want other JavaScript files to be able to access this data easily.

Use Hype Pro and a Persistant Symbol. That will allow you to keep the element on the screen across multiple scenes.

That's as simple as changing the variable data...

You can do this...

window.score = window.score + 1; // plus one
window.score = window.score - 1; // minus one

You could also do this...

window.score++; // plus one
window.score--; // minus one

The trick is to use a JavaScript event. There are many ways to do this. That depends on the design of your project. You could have separate JavaScript files that do nothing but change the score. Then, you can run those scripts with Hype's events. You could have a single JavaScript file that controls all of the action, using JavaScript events to monitor the action. The latter is probably more difficult, but it gives you greater control.

The JavaScript would be triggered on an event and then update the score. You'd basically need to edit the innerHTML of the score related element.

I don't usually create example templates in situations like this, as that's how you learn. You don't get muscles by having someone else do the pushups for you. However, this information should be a good boost toward the direction you want to go.

However, if no one creates a template for you and you're still having trouble creating one yourself, I might... MIGHT... create a Free Hype Template. There are about nine of them mentioned here...

5 Likes