Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Help with CopperCube
Countdown Time in CopperCube

dynamogames
Registered User
Quote
2023-04-21 09:53:59

I'm trying to create a count down timer in CopperCube where in each level the player has a particular time e.g 10minutes to finish a level and if the player does not finish the level before the set time, game over and also the timer would be shown to the player as a 2D overlay. I'm trying to create the countdown timer in CopperCube but I can't seem to get it.


sven
Registered User
Quote
2023-04-21 10:22:17

I probably make action for this soon (because i need this also)

But you can just declare variable for example
CountdownTimer=-1

To start it set value to CountdownTimer

Now on every X ms do something..
If CountdownTimer !=-1 substract value..
If it reaches to 0 execute action and set it -1 ( so its disabled)

On overlay you can display it as $CountdownTimer$

Edit: with some math you can convert it to minutes and seconds also..


okeoke
Registered User
Quote
2023-04-21 10:40:17

Hi, or you can add a behavior to a 2d overlay:)
// The following embedded xml is for the editor and describes how the behavior can be edited:
// Supported types are: int, float, string, bool, color, vect3d, scenenode, texture, action
/*
<behavior jsname="behavior_CoutnDownTimer" description="Countdown timer">
<property name="timerMs" type="int" default="10000" />
<property name="actionOnRunOut" type="action" default="" />
<property name="textPrefix" type="string" default="Time: " />
</behavior>
*/

behavior_CoutnDownTimer = function () {
this.initTime = null;
this.active = true;
};

behavior_CoutnDownTimer.prototype.onAnimate = function (node, timeMs) {
if (!this.lastTime) {
this.lastTime = timeMs;
return;
}

if (this.active) {
var delta = timeMs - this.lastTime;
this.lastTime = timeMs;
if (delta > 200) delta = 200;

this.timerMs -= delta;
if (this.timerMs <= 0) this.timerMs = 0;

var date = new Date(this.timerMs);
var mins = date.getMinutes();
var secs = date.getSeconds();
var text = this.textPrefix + strPadLeft(mins, '0', 2) + ':' + strPadLeft(secs, '0', 2); + '\r\n';

ccbSetSceneNodeProperty(node, 'Text', text);

if (this.timerMs <= 0) {
this.active = false;
ccbInvokeAction(this.actionOnRunOut);
}
}
}

function strPadLeft(string, pad, length) {
return (new Array(length + 1).join(pad) + string).slice(-length);
}


Example: https://drive.google.com/file/d/...


dynamogames
Registered User
Quote
2023-04-22 01:37:25

Thanks @Sven and @Okeoke
@Okeoke, your behavior works properly but I noticed that when the timer reaches 00:00, the action does not take place, in the behaviour you sent in the drive link, after time runs out, it should print "Time out", but it doesn't print time out, it just get stuck in the " Time: 00:00" text, I tried adding another action, which was the Hide Scene Node action to hide the 2d overlay after the time runs out but this doesn't work too.


okeoke
Registered User
Quote
2023-04-22 09:17:40

Hi @dynamogames
Have you tried extensively choosing the overlay node instead of "the current scene node"? Both hiding the node and changing its text works for me that way:
🔎︎

But "the current scene node" weirdly enough actually doesn't work.


okeoke
Registered User
Quote
2023-04-22 11:53:52

Ok, it's actually my bad. I just checked the js reference
ccbInvokeAction(actionId, currentNode)
invoke action actually has second argument - currentNode. That's why it's not working:)

You can easily fix it by updating line 39:
ccbInvokeAction(this.actionOnRunOut);
to
ccbInvokeAction(this.actionOnRunOut, node);


dynamogames
Registered User
Quote
2023-04-25 21:33:13

Thanks for the contributions okeoke, but in the js file, the code does not reach line 39 but stops at line 37, I tried creating a new line for the code "ccbInvokeAction(this.actionOnRunOut, node);" but the error still persists.


hadoken
Guest
Quote
2023-04-26 00:44:18

@okeoke thank you very much for sharing!

@dynamogames, okeoke's first Google Drive example seems to contain an old version of okeoke's behavior yet giving as option but still not containing "ccbInvokeAction(this.actionOnRunOut)",

here you can find a hopefully fixed version (additionally fixed small typo in behavior's name):
https://www.dropbox.com/s/bw21hu...


guest
Guest
Quote
2023-04-26 13:21:35

THX


dynamogames
Registered User
Quote
2023-09-13 23:08:58

Back to this thread :-), @okeoke and @hadoken, is there any way to record the time, let's say when player is in proximity to an object and save it in a variable which can be displayed on a 2D overlay.


VP
Guest
Quote
2023-09-14 07:40:36

Here's how to do it without coding....

// Starts a timer when node enters the zone:
On Proximity (Enters)
Set Variable "TimerActive" =1

// Stops (and resets) the timer when node leaves the zone:
On Proximity (Leaves)
Set Variable "TimerActive" =0,
Set Variable Time" =0

// Updates a 2D overlay to display current time in seconds:
Every few seconds (1000ms)
If Variable "TimerActive" =1
Set Variable "Time" +1
Update 2D Overlay "Time= $Time$"


serge
Registered User
Quote
2023-09-16 13:39:52

I created a game the timer countdown and shows game over.


serge
Registered User
Quote
2023-09-16 13:42:39

dynamogames wrote:
I'm trying to create a count down timer in CopperCube where in each level the player has a particular time e.g 10minutes to finish a level and if the player does not finish the level before the set time, game over and also the timer would be shown to the player as a 2D overlay. I'm trying to create the countdown timer in CopperCube but I can't seem to get it.



Is this what you trying to do?
https://youtu.be/au9vPiG_r5E?si=...


dynamogames
Registered User
Quote
2023-09-16 20:32:21

Yes exactly Serge, but I have the countdown timer implemented, I'm just trying to find a way to get players best time on proximity to a certain object, everytime they play, this best time is recorded and shown to player so that they can try and beat it.


dynamogames
Registered User
Quote
2023-09-16 20:43:08

@VP thanks for the contribution


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Interna?ional" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact