Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
behavior_2DJumpNRun.js

fraktal
Registered User
Quote
2018-04-27 23:55:08

Im trying to implement running to the 2DJumpNRun script by detecting if shift was pressed.

It works if i use another key than shift... but how to implement shift key detection..

The script returns now as the event.shiftKey was not defined...

Should i use event.shiftKey differently as a defined global variable that ?


behavior_2DJumpNRun = function()
{
this.ForwardKeyDown = false;
this.BackKeyDown = false;
this.PressedJump = false;
this.RunKeyDown = false;
this.LastTime = null;
this.JumpForce = 0;
this.JumpLengthMs = 1000;
};

// called every frame.
// 'node' is the scene node where this behavior is attached to.
// 'timeMs' the current time in milliseconds of the scene.
// Returns 'true' if something changed, and 'false' if not.
behavior_2DJumpNRun.prototype.onAnimate = function(node, timeMs)
{
// get the time since the last frame

if (this.LastTime == null)
{
this.LastTime = timeMs; // we were never called before, so store the time and cancel
this.InitPos = ccbGetSceneNodeProperty(node, 'Position');
this.InitRotation = ccbGetSceneNodeProperty(node, 'Rotation');
return false;
}

this.LastNodeUsed = node;

var delta = timeMs - this.LastTime;
this.LastTime = timeMs;
if (delta > 200) delta = 200;

// move

var pos = ccbGetSceneNodeProperty(node, 'Position');
pos.z = this.InitPos.z; // force to by always on the same 2D axis

if (this.ForwardKeyDown)
{
pos.x += this.Speed * delta;
ccbSetSceneNodeProperty(node, 'Animation', 'walk');
ccbSetSceneNodeProperty(node, 'Rotation', this.InitRotation);
}
else
if (this.RunKeyDown)
{
pos.x += this.Speed * delta;
ccbSetSceneNodeProperty(node, 'Animation', 'run');
ccbSetSceneNodeProperty(node, 'Rotation', this.InitRotation);
}
else
if (this.BackKeyDown)
{
pos.x -= this.Speed * delta;
ccbSetSceneNodeProperty(node, 'Animation', 'walk');
ccbSetSceneNodeProperty(node, 'Rotation', this.InitRotation.x, this.InitRotation.y - 180, this.InitRotation.z);
}
else
{
// not walking, stand

ccbSetSceneNodeProperty(node, 'Animation', 'stand');
}

// jump if jump was pressed

if (this.PressedJump && this.JumpForce == 0)
{
this.PressedJump = false;
this.JumpForce = this.JumpLengthMs;
}

if (this.JumpForce > 0)
{
pos.y += this.JumpSpeed * delta;
this.JumpForce -= delta;

if (this.JumpForce < 0)
this.JumpForce = 0;
}


// set position

ccbSetSceneNodeProperty(node, 'Position', pos);

return true;
}

// parameters: key: key id pressed or left up. pressed: true if the key was pressed down, false if left up
behavior_2DJumpNRun.prototype.onKeyEvent = function(key, pressed)
{
// store which key is down
// key codes: left=37, up=38, right=39, down=40, A=65, D=68, W=87, S=83, shift=16 /
// event.shiftKey (?)

if (key == 65)
this.BackKeyDown = pressed;
else
if (key == 68)
this.ForwardKeyDown = pressed;

// jump when space or W pressed

if (key == 32 && pressed || key == 87 && pressed)
this.PressedJump = true;

// run when shift pressed
if (event.shiftKey==1)
this.RunKeyDown = pressed;
}


wild-master
Registered User
Quote
2018-04-28 01:55:47

Hello. I realized that the problem happened because CopperCube's Key Code for the Shift button is 160 instead of the standard 16 that appears on JavaScript websites.

I also made the Run feature work in both directions.

Try this:
// This is a coppercube behavior which moves the node it is attached to only on the x axis,
// controlled by the cursor keys and with space for 'jump'.
//
// 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_2DJumpNRun" description="Simple 2D Jump'n'Run Player">
<property name="Speed" type="float" default="0.02" />
<property name="JumpSpeed" type="float" default="0.2" />
<property name="JumpLengthMs" type="int" default="200" />
</behavior>
*/

behavior_2DJumpNRun = function()
{
this.ForwardKeyDown = false;
this.BackKeyDown = false;
this.PressedJump = false;
this.RunKeyDown = false;
this.LastTime = null;
this.JumpForce = 0;
this.JumpLengthMs = 1000;
};

// called every frame.
// 'node' is the scene node where this behavior is attached to.
// 'timeMs' the current time in milliseconds of the scene.
// Returns 'true' if something changed, and 'false' if not.
behavior_2DJumpNRun.prototype.onAnimate = function(node, timeMs)
{
// get the time since the last frame

if (this.LastTime == null)
{
this.LastTime = timeMs; // we were never called before, so store the time and cancel
this.InitPos = ccbGetSceneNodeProperty(node, 'Position');
this.InitRotation = ccbGetSceneNodeProperty(node, 'Rotation');
return false;
}

this.LastNodeUsed = node;

var delta = timeMs - this.LastTime;
this.LastTime = timeMs;
if (delta > 200) delta = 200;

// move

var pos = ccbGetSceneNodeProperty(node, 'Position');
pos.z = this.InitPos.z; // force to by always on the same 2D axis

if (this.ForwardKeyDown)
{
if (this.RunKeyDown) {
pos.x += this.Speed * delta * 2;
ccbSetSceneNodeProperty(node, 'Animation', 'walk');
ccbSetSceneNodeProperty(node, 'Rotation', this.InitRotation);
} else {
pos.x += this.Speed * delta;
ccbSetSceneNodeProperty(node, 'Animation', 'walk');
ccbSetSceneNodeProperty(node, 'Rotation', this.InitRotation);
}
}
else
if (this.BackKeyDown)
{
if (this.RunKeyDown) {
pos.x -= this.Speed * delta * 2;
ccbSetSceneNodeProperty(node, 'Animation', 'walk');
ccbSetSceneNodeProperty(node, 'Rotation', this.InitRotation.x, this.InitRotation.y - 180, this.InitRotation.z);
} else {
pos.x -= this.Speed * delta;
ccbSetSceneNodeProperty(node, 'Animation', 'walk');
ccbSetSceneNodeProperty(node, 'Rotation', this.InitRotation.x, this.InitRotation.y - 180, this.InitRotation.z);
}
}
else
{
// not walking, stand

ccbSetSceneNodeProperty(node, 'Animation', 'stand');
}

// jump if jump was pressed

if (this.PressedJump && this.JumpForce == 0)
{
this.PressedJump = false;
this.JumpForce = this.JumpLengthMs;
}

if (this.JumpForce > 0)
{
pos.y += this.JumpSpeed * delta;
this.JumpForce -= delta;

if (this.JumpForce < 0)
this.JumpForce = 0;
}


// set position

ccbSetSceneNodeProperty(node, 'Position', pos);

return true;
}

// parameters: key: key id pressed or left up. pressed: true if the key was pressed down, false if left up
behavior_2DJumpNRun.prototype.onKeyEvent = function(key, pressed)
{
// store which key is down
// key codes: left=37, up=38, right=39, down=40, A=65, D=68, W=87, S=83, shift=16 /
// event.shiftKey (?)

if (key == 65)
this.BackKeyDown = pressed;
else
if (key == 68)
this.ForwardKeyDown = pressed;

// jump when space or W pressed

if (key == 32 && pressed || key == 87 && pressed)
this.PressedJump = true;

// run when shift pressed
if (key == 160)
this.RunKeyDown = pressed;
}



wild-master
Registered User
Quote
2018-04-28 02:16:29

(I needed to split the message because this forum has a character limit)

I changed your Run feature so it works in both directions. If you don't like what I did, all you have to do is use the script you already have and just change the number 16 in your code to 160. You don't have to copy and paste my code unless you like what I did with the Run feature.

The normal speed of the movement uses this code:
this.Speed * delta

And if the Run button is pressed, the speed will be increased by using this patch of code instead:
this.Speed * delta * 2



wild-master
Registered User
Quote
2018-04-28 02:34:41

For all people who want to know the Key Codes of other keyboard keys, I'll show this method for finding every Key Code:

1. Start a new empty project.
2. Click on any scene node.
3. Click the Behavior tab for that scene node.
4. Add the Behavior called "Every few seconds, do something".
5. Set its MS interval to 1.
6. Add the Action called "Execute JavaScript".

7. Add this code in the JavaScript window:
ccbRegisterKeyDownEvent("keyPressedDown");

function keyPressedDown(keyCode)
{
print("A key was pressed down:" + keyCode);
}
8. Now test the project and press your keyboard buttons, and the Key Codes will appear in the window.


fraktal
Registered User
Quote
2018-04-28 12:45:47

That worked wonders! And the implementation for speed and directions.. :)


fraktal
Registered User
Quote
2018-04-30 11:35:10

For reference and to bring this further.. How can this be made to work in WebGL target?

For keycode 160 or event.Shiftkey to be detect when pressed?


wild-master
Registered User
Quote
2018-04-30 16:18:02

I tested for you and I noticed that the Key Code for the Shift Key for WebGL is still 16, even though it's 160 for the Windows target.


To fix this problem, change the last piece of the script from this:
// run when shift pressed
if (key == 160)
this.RunKeyDown = pressed;
}
To this:
// run when shift pressed
if (key == 160 || key == 16)
this.RunKeyDown = pressed;
}



fraktal
Registered User
Quote
2018-04-30 22:01:20

Ah i see.. Thank you for the response. That is what i needed to find.


Create reply:


Posted by: (you are not logged in)


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