Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
"Execute Javascript" action question...

Bogo
Guest
Quote
2016-08-24 16:14:31

Hi, i'm sorry if my question is stupid, but iI want to create an action when a button "A" is pressed then execute

alert("hello world!");

but when I copy this into the "execute javascript" I get this error when i play the game and press "A": "ReferenceError: alert is not defined".


I want to execute this javascript and when press "A" key, a little window is displayed, but this wouldn't happen :(


Sorry for my bad english.


pmax
Registered User
Quote
2016-08-24 17:02:55

You must use the "print ()".


just_in_case
Moderator
Quote
2016-08-24 18:41:27

I think that is available for editor only ..... You can execute this command alert ("hello world") from scripting window available in the editor but can't execute in the final executable programme...


Bogo
Guest
Quote
2016-08-25 07:44:58

Thanks guys!

So, it is impossible to generate a little window (like pop-up) during the game, when someone press a button?


pmax
Registered User
Quote
2016-08-25 07:57:57

You can use the command print (), or graphically, you can make appear or disappear a "2D Overlay" with the desired written.


Bogo
Guest
Quote
2016-08-25 08:54:11

Thank you.

I'm sorry but i have another question: I want to count objects with the mouse (when click, count). I did it, but the problem is that you can count every object multiple times if you click on them multiple times.

I would like to do that you can only do this once per object.

For example if I have 3 objects (A, B and C) and I click on object A, then 2 times on object B and one time on C, my 2d overlay will display 4, and not 3, and that's a mistake.

Now it's like this:
"When clicked on this do something"
action: "set or change a variable" --> add 1 to "varName".


It works but I don't know how to disable multiple times counting on a object.

I want to do that when you count an object, you can't count it again.

Sorry for the inconvenience...


just_in_case
Moderator
Quote
2016-08-26 04:05:32

Hey bogo you can use if a varuable has value do something for example you have 4 objects and you dont want your click counter to get higher than value 4...

Then you can simply use this action if a variable "varname" is more than 4, then set or change variable "varname" to 4..


Just increase the value 4, according to the objects present in ur scene...

Hope that works...


Bogo
Guest
Quote
2016-08-26 08:37:32

Thank you just_in_case,
that's a good idea, but it would be a "bug" for this game, because one of the goals would be to count the exact number of objects, but if you do this, someone could click 4 times on an object and win the goal...

I tried the "restart behaviors of a scene node" action after a click (i tought that after this, it wouldn't count if you click again), but it didn't work


pmax
Registered User
Quote
2016-08-26 09:59:43

it is probably a bug, I sent a sample scene Niko I'm waiting for an answer.


just_in_case
Moderator
Quote
2016-08-26 10:28:33

That is not the bug...that behavious is working fine ...


You can also hide the object after a click....

You have to create another action set variable" cliked _bject_1" = 1 and then assign the action if a variable cliked_object_1 has a value equal to 1 then set or change variable "click counter" add value +1..

And also have to use when mouse released up set variable clikced_object_1 = 0

You have to assign these actiona to all the objects present in ur scene...
Variable cliked_object_1 should be changed according to the object names present in ur scene...

Don't change the name of the variable click counter... Leave it same in all objects..

Hope you understand what am trying to say...

Just create to instance of an object one in on mouse down and another is on mouse up...

And add behaviour set or change variable ... One for click counter... Another one for tiggering click effect...


Bogo
Guest
Quote
2016-08-26 11:03:39

Wow just_in_case... you're a genius

Thank you guys :) you resolved my problem


theBikerJC
Guest
Quote
2016-10-05 09:02:11

Me too I have a question about "execute javascript".

I want to check if a variable has a "substring".


example

var myVariable = "hello buddy"
var secondVariable = null

if there is "buddy" in myVariable, than set the value of secondVariable to "buddy".

Is this possible in coppercube? can you give me please the code?

Thanks


erik
Registered User
Quote
2016-10-05 11:55:07

Yes, use something like

if (myVariable.indexOf('buddy') != -1)
{
secondVariable = 'buddy';
}



theBikerJC
Guest
Quote
2016-10-05 14:01:31

Thank you Erik!

I tried your solution but it won't work.

Script.1.ReferenceError.myVariable is not defined



I have set ,,before first drawing do something'' and I have set variables myVariable and secondVariable.

So I can't understand where i'm wrong?


niko
Moderator
Quote
2016-10-06 03:03:58

theBikerJC, maybe you are new to JavaScript?
You need to declare your variables, just as you wrote above in your example. The code snipped posted by erik above is just example code, it isn't the full code you would need, you would have to adjust it to your needs.


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