Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
The action is not really unregistered

dekon_17
Registered User
Quote
2022-03-09 16:40:01

I can't belive I'll say this, but I actually tried to programm some basic physics. The only reason why I did this is that I wanted to create a runner game for my phone since going to PlayMarket... Uhh, let's don't talk about that. SO, despite the fact that this formula works, I have a bit of a problem though.

The jump itself is an action, and this action has a function that is being registered on frame. Everything is fine, until the moment my character lands. When it does reach 0 units by Y axis (h <= 0), the position is changed to 0,0,0 (the game doesn't feature built-in collision behavior, so, yeah, that's the reason for this to be). And that's when I noticed something:
🔎︎

it doesn't work bruuuuuuuuuuuuuuuuuuu...

Speaking of "doesn't work", I don't know what is wrong... Yeah, so, um-m, I guess I should ask help about that. Here's the code to understand better:
/*
<action jsname = "action_MG1jump" description = "Jumping for MG1">

<property name = "Plr" type = "scenenode"/>

</action>
*/

action_MG1jump = function()
{
}

action_MG1jump.prototype.execute = function()
{
var OnGround = ccbGetCopperCubeVariable ("OnGround");

if (OnGround == 1)
{
ccbSetCopperCubeVariable ("OnGround", 0);

this.t0 = (new Date()).getTime();
this.h0 = ccbGetSceneNodeProperty(this.Plr, "Position");

var me = this;
this.registeredFunction = function() { me.Jump(); };
ccbRegisterOnFrameEvent(me.registeredFunction);
};
}

action_MG1jump.prototype.Jump = function()
{
var t1 = (new Date()).getTime();

var t = (t1 - this.t0) / 1000;

var h = this.h0.y + 30 * t - (10 * t * t) / 2;

print (h);

ccbSetSceneNodeProperty(this.Plr, "Position", 0, h, 0);

if (h <= 0)
{
var me = this;
this.registeredFunction = function() { me.Jump(); };
ccbUnregisterOnFrameEvent(this.registeredFunction);

ccbSetSceneNodeProperty(this.Plr, "Position", 0, 0, 0);
};
}

As far as I know, there is some stuff like "retrun true" or "return false" does exist, but I still don't understand how does these things work and how to use them.


sven
Registered User
Quote
2022-03-09 19:04:18

The return statement ends function execution and specifies a value to be returned to the function caller.


/*
<action jsname = "action_MG1jump" description = "Jumping for MG1">

<property name = "Plr" type = "scenenode"/>

</action>
*/

action_MG1jump = function()
{
}

action_MG1jump.prototype.execute = function()
{
var OnGround = ccbGetCopperCubeVariable ("OnGround");

if (OnGround == 1)
{
ccbSetCopperCubeVariable ("OnGround", 0);
print("STARTED..");//REMOVE THIS LINE
this.t0 = (new Date()).getTime();
this.h0 = ccbGetSceneNodeProperty(this.Plr, "Position");

var me = this;
this.registeredFunction = function() { me.Jump(); };
ccbRegisterOnFrameEvent(me.registeredFunction);
}
}

action_MG1jump.prototype.Jump = function()
{
var t1 = (new Date()).getTime();

var t = (t1 - this.t0) / 1000;

var h = this.h0.y + 30 * t - (10 * t * t) / 2;

print (h);

ccbSetSceneNodeProperty(this.Plr, "Position", 0, h, 0);

if (h <= 0)
{
print("DONE");//REMOVE THIS LINE
ccbSetCopperCubeVariable ("OnGround", 1);//REMOVE THIS LINE
ccbSetSceneNodeProperty(this.Plr, "Position", 0, 0, 0);
ccbUnregisterOnFrameEvent(this.registeredFunction);
}
}



csp-games
Guest
Quote
2022-03-09 19:20:09

I code in a simple style. I could never wrap my head around the way you guys are doing stuff. Prototype? I even don't know what that's good for. I use:

function myfunction()
{
do something
}
ccbRegisterOnFrameEvent(myfunction);

Works flawlessly for me. That said, it's called in the before-first-drawing behavior.

Besides, notice this will not stop when the scene is switched. It's continued, and I'm not sure if scenenodes of the same name inside different scenes could cause mayhem.


dekon_17
Registered User
Quote
2022-03-10 06:19:04

To sven:

Woah, this kinda works... Can't say that I really understood what caused it, but, uh-h... Whatever, as someone said once:
It just works

Yet again, thanks for help.

To csp-games:

Well, actually, I don't know which way is simplier or not, I came here with no programming knowlege at all (some Pascal lessons from scool doesn't count), so, I am doing the stuff like most of the people do. Hell, my first scripts were just a combination of parts from other scripts by other people. So, I might be doing something in a not very simple or a good way.


csp-games
Guest
Quote
2022-03-14 13:31:27

While I keep on using code that others deem to be no longer good style. Yet it works. Sometimes better than all the sophisticated stuff. The thing with JS is, you can use many different styles. Whether you say "i++;" (that's C++) or "i=i+1;" (that's BASIC), it works both ways. Readability is important to me. And I wanna stay in control, so I rarely use "For Each" and stuff like that. Even array.length I use rarely, but would rather know already the size of an array, because I have made it.

BTW did I mention that I personally know the guy who made Pascal? Professor Nick Wirth.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "I?ternational" (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