problem with return true and variable

Get help using Construct 2

Post » Sun Nov 20, 2011 3:19 am

Hey a problem of javascript again ;)
I did a condition

pluginProto.cnds = {};
var cnds = pluginProto.cnds;
     
cnds.ConfirmOKclicked = function ()
{
    if (confirmation=1)
    {
    return true;     
    }
    else
    {
    return false;
    }
};


confirmation is a var (its value change via actions) that I defined at the beginning of the runtime as following:

var confirmation;

I tried too:
var confirmation=0;

The problem is when the condition is used, from the beginning of the application, the return is obligatory true instead to be false...

Thanks
B
44
S
12
G
13
Posts: 628
Reputation: 10,471

Post » Sun Nov 20, 2011 4:27 am

In instanceProto.onCreate = function()
Make a variable this.confirmation = false;

In cnds.ConfirmOKclicked
return this.confirmation;

this. makes that the variable is intriseque part of the plugin's runtime and its value is stored over the several blocks of code.

The use of variable (their declaration and use) is directly tied to javascript.
this. is also a "standard function" of Javascript.

The SDK builds on this to make you use
this.variable_name; to store your values and have access to them within the plugin, even once it is sealed.
This is covered in the SDK - extract.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 7,000
Reputation: 57,795

Post » Sun Nov 20, 2011 4:40 pm

[code]if (confirmation=1)[/code]
A single = means assignment, so this statement means "assign 1 to confirmation and return 1", and since 1 is true the if statement is always true. You need two or three equals signs (e.g. confirmation === 1) to perform equality testing, generally three but make sure you're aware of the difference between using == and ===.

This is a basic Javascript language question - it's best not to ask about the Javascript language itself in the forum, because there are much better places to learn Javascript on the web. From the SDK docs:

[quote]Some useful resources for javascript are:

Mozilla's Javascript Guide - https://developer.mozilla.org/en/JavaScript/Guide
A complete guide to javascript. This might be a good starting point if you are new to programming.

StackOverflow - http://www.stackoverflow.com
An excellent Q&A website. Also a good place to search to see if your question has already been asked and answered.

Javascript Garden - http://bonsaiden.github.com/JavaScript-Garden/
Guide to the unusual parts of javascript. Very useful if you have experience with a different programming language but are new to javascript.

Mozilla Developer Network (MDN) - https://developer.mozilla.org
An excellent reference for HTML, javascript, and more. A very useful place to look up features for the browser side of the plugin.[/quote]Ashley2011-11-20 16:40:36
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600


Return to How do I....?

Who is online

Users browsing this forum: h1k3 and 16 guests