Newbie help with JavaScript buttons


(Steve Caplin) #1

I’m reasonably familiar with Hype, but Javascript has got me beat. Here’s what I want to do:

  1. Set an action on a graphic (actually a button) so that when clicked, the button changes to a different state - using different artwork.

  2. I then want to be able to click another button that will evaluate the state of the 6 main buttons, reporting whether or not the correct ones have been clicked. If they have, I want to go to one scene, if they haven’t, I want to reset and restart the current scene.

Does anyone have the patience to tell me what to do?

Thanks in advance

Steve


(Steve Caplin) #2

Really? No-one?


(Hans-Gerd Claßen) #3

there is no common answer.

if you’re asking for a script try to post a file with required setup. the description is not clear for me.

in your case i would try to accomplish a setup that works with custombehaviours. you can do a lot with it, though not everything …


(Steve Caplin) #4

Thanks for replying.

The main thing I need to do is for a script to monitor the state of six buttons, determining if they have or have not been pressed. I don’t think I can do this with custom behaviours, can I?


(Hans-Gerd Claßen) #5

the simplest approach is to assign an id or class to every button and on touchstart or click start a script that defines a global var for it as clicked (true). results in:

myGlobal = {
"thisId" : true,
"anotherId" : true
}

then on [quote=“SteveC, post:1, topic:8494”]
another button
[/quote]

iterate thru this object and check for

if(myGlobal["thisId"])

to collect the ones that have been clicked


(Hans-Gerd Claßen) #6

in real code :wink:

//assign any class to some buttons we will get this class here:
var thisClass = element.classList.item(1);

//check if var exists
if(typeof myGlobal === 'undefined')
{
//create it if not
myGlobal = {}
}

//the checkbutton id expected of class 'check'
if(thisClass === 'check')
{
//get all keys and join them to a string
var allKeys = Object.keys(myGlobal).join(', ');
//write the result in a div with class 'result'
document.getElementsByClassName('result')[0].innerHTML = allKeys;
//reset the global
myGlobal = {};
}else{
//reset the result
document.getElementsByClassName('result')[0].innerHTML = '';
//create a property for the clicked button
myGlobal[thisClass] = true;
}

assign this script to every button onClick or touchStart


#7

Hi Steve!

I am not clear from your description about evaluating the states of the buttons. Do all the buttons need to be clicked and then an evaluation is made - or as soon as one wrong button is clicked the scene should reset itself? Does this data need to be retained~used for purposes other than triggering a scene reset or change?

If it is the latter option (“one wrong button”), and the data does not need to be retained - then I think it might be possible to craft a simple non-javascript solution using timelines.

@h_classen: I keep learning new things from your posts (“Object.keys” in this case) - Thank You!


(Steve Caplin) #8

Hans-Gerd: thank you, this is extremely helpful!

JimScott: It’s the former. The user can press any number of the six buttons, and only if the correct three are pressed does Hype go to the next scene; otherwise, it clears the pressed buttons and the user starts again. It’s for a game I’m devising.


#9

Hi Steve!

Well, I couldn’t resist. If Hans solution works well for You excellent… in any event here is another approach - mainly timeline based - but I did need a wee bit of JavaScript.

Project Demo: HypeButtons_JHSv1.hype.zip (46.8 KB) - Demo here.

Assumption in this Demo: If there are only three correct answers there is no reason to wait for a fourth button click, as that would automatically doom the player with at least one wrong answer. You could allow for more buttons clicked but it is not the case here; after the third button is clicked an evaluation is made. Nor have I bothered to prevent the same button from being clicked twice (though easy enough to do).

Two timelines:
One tracks the total number of button clicks (“Btn Total”) - in this Demo (3) clicks is the magic number.
One tracks the total number of right answers (“Correct Answer”).

Each time a button is clicked the “Btn Total” timeline advances. Each time a “correct answer” button is clicked the (you guessed it) “Correct Answer” timeline advances. Three correct answers will advance the “Correct Answer” timeline to 1 second.

After a third button has been clicked, an action on the “Btn Total” timeline runs triggering a JavaScript “if… else” function that checks to see if the “Correct Answer” timeline is at 1 second ( = three “correct answer” clicks). If it is 1 second then we go to Scene 2 otherwise back to a reset Scene 1.


(Steve Caplin) #10

Hi Jim

Sorry only just seen this excellent response… I think this is precisely what I need. Many, many thanks for taking the trouble!

Steve


(Christos Kentrotis) #11

Hello Hans,

I think I have a similar problem with Steve. I 've tried to copy your code in my project and play a timeline when the 6 green buttons are clicked but with no luck. I 'm attaching the file. As you can see from my efforts, I don’t know javascript but I still try! If you believe that you could help me with no big effort, I would appreciate it.

Thank you in advance,
Christos

play a timeline when 6 buttons clicked.hype.zip (2.7 MB)


(Hans-Gerd Claßen) #12

hello @kentro73,

your document is throwing a bunch of javacsript-errors, so i did not try anything with it …

But it short run sthg like this on click/touchstart of a correct button

//common class is 'check':
var thisClass = element.classList.item(1);
//check if global obj exists
if(typeof myGlobal === 'undefined') myGlobal = {};



if(thisClass === 'check')
//the advantage of storing in a object instead of array is that multiple clicks on the same element are not counted ...
myGlobal[element.id] = true;
if(Object.keys(myGlobal).length === 6)hypeDocument.startTimelineNamed('bravo', hypeDocument.kDirectionForward);

(Christos Kentrotis) #13

Hans, you are SUPER!

Thank you very much! My project is about an app for iPad, for learning Greeks in Kids of 5-7 years old.
It’s about the presentation of my postgraduate work for my Diploma in the Hellenic Open University.

As I don’t know coding and you helped me (not only this time, but I 've used another code of you on the blog about drag & drop in a specified position), I wonder if you are interested to be credited in my presentation as a person that helped me work it out.

When the app is ready, I 'll send you a demo to see and tell me if you are interested in my proposition.

Thanks again for your help!


(Hans-Gerd Claßen) #14

nice … forumhelp is always for free :slight_smile: no need to mention anywhere …


(Christos Kentrotis) #15

Ok, thanks a lot

:+1:


(Christos Kentrotis) #16

Hello Hans,

I used the code you sent me on the next scenes too, but only the first time works well. When it jumps to the next scene and after clicking the correct buttons, it doesn’t play the “bravo” timeline. I’ve tried to preview just the scene with the problem but it’s working well. The problem appears again on the next scene. Can you figure out where is the problem?
I’m attaching the file.

play a timeline when 6 buttons clicked next scene problem.hype.zip (471.6 KB)


(Hans-Gerd Claßen) #17

if you wanna reuse those lines on different scenes you’ll have to reset the var storing the results …

//common class is 'check':
var thisClass = element.classList.item(1);
//check if global obj exists
if(typeof myGlobal === 'undefined') myGlobal = {};



if(thisClass === 'check'){
//the advantage of storing in a object instead of array is that multiple clicks on the same element are not counted ...
myGlobal[element.id] = true;
if(Object.keys(myGlobal).length === 6){
hypeDocument.startTimelineNamed('bravo', hypeDocument.kDirectionForward);
myGlobal = {};
}
}

(Christos Kentrotis) #18

OMG! I feel idiot. I saw it a lot of times and you have just changed a word? :pray: