Highlighted

Multiple games with same script on timeline

New Here ,
May 28, 2019

Copy link to clipboard

Copied

I have an HTML5/js  project that contains the Main UI and four types of games (drag and drop;...etc) and a script for each type of game, that types will be repeated in the project 80 times.

To do that. I make  80 movieclips in the lib and copy/paste the script that corresponds to each type of game on the timeline..which results in a big js file with multiple repeated scripts.

Is there a best strategy for doing that?

Adobe Community Professional
Correct answer by kglad | Adobe Community Professional

no.

among the several ways you could condense your js file is to create one function that encodes each game. put that function on frame one.

pass the parameters (that vary from game to game) to that one function from the frames that contain your 80 games.  ie, use 80 function calls.

Views

221

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Multiple games with same script on timeline

New Here ,
May 28, 2019

Copy link to clipboard

Copied

I have an HTML5/js  project that contains the Main UI and four types of games (drag and drop;...etc) and a script for each type of game, that types will be repeated in the project 80 times.

To do that. I make  80 movieclips in the lib and copy/paste the script that corresponds to each type of game on the timeline..which results in a big js file with multiple repeated scripts.

Is there a best strategy for doing that?

Adobe Community Professional
Correct answer by kglad | Adobe Community Professional

no.

among the several ways you could condense your js file is to create one function that encodes each game. put that function on frame one.

pass the parameters (that vary from game to game) to that one function from the frames that contain your 80 games.  ie, use 80 function calls.

Views

222

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 28, 2019 0
Adobe Community Professional ,
May 28, 2019

Copy link to clipboard

Copied

no.

among the several ways you could condense your js file is to create one function that encodes each game. put that function on frame one.

pass the parameters (that vary from game to game) to that one function from the frames that contain your 80 games.  ie, use 80 function calls.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2019 1
Adobe Community Professional ,
May 28, 2019

Copy link to clipboard

Copied

I'd be really surprised if each game is run by a single function.

Wrapping each game's code up in a module would be the ideal solution.

https://coryrylan.com/blog/javascript-module-pattern-basics

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2019 1
Adobe Community Professional ,
May 28, 2019

Copy link to clipboard

Copied

the op stated, ".. a script for each type of game, that types will be repeated in the project 80 times."

that's unclear (to me) but if it means the code is similar for each of the four types of games then my answer stands.  (except it might be easier and better to user four functions instead of one with one function for each game type.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2019 0
Engaged ,
May 28, 2019

Copy link to clipboard

Copied

kglad, do you mean like global functions in the Global section instead of the functions being in each individual game/movie clip? or is that bad practise?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2019 0
Adobe Community Professional ,
May 28, 2019

Copy link to clipboard

Copied

you could do that, but not it's not necessary.

on frame 1:

var tl=this;

function dragAndDropF(drag_mcA,drop_mcA,success_frame,fail_frame){

for(var i=0;i<drag_mcA.length;i++){

tl[drag_mcA].addEventListener('pressmove',downF);

tl[drag_mcA].addEventListener('mouseup',upF);

tl[drag_mcA]].target_mc=tl[drop_mcA];

}

downF=function(e){

e.currentTarget.x = (e.stageX)/stage.scaleX ;

e.currentTarget.y = (e.stageY)/stage.scaleY;

stage.update();

}

upF=function(e){

//evaluate for correct drop etc

}

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2019 2
New Here ,
May 29, 2019

Copy link to clipboard

Copied

Hi, thank you for your replies.

sorry if my question was unclear (English isn't my language). perhaps the right title should be "Multiple games with just four scripts on the timeline".

I found very interesting the solution proposed by Kglad  , I have to change my codes to try it.  (I'm used to AS3 , I am new to js , so I hope the access of movieclips from the main timeline won't be a nightmare for me : (

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 29, 2019 0
kglad LATEST
Adobe Community Professional ,
May 29, 2019

Copy link to clipboard

Copied

actionscript and javascript and similar in many ways, especially if you're familiar with coding on the timeline (as opposed to using class files).

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 29, 2019 0
Adobe Community Professional ,
May 28, 2019

Copy link to clipboard

Copied

Seems to me that if you have several types of games, each type should be in a different function.

This is what I do for example in the same fla if I have

1- student inputs an answer in input field

2- multiple-choice question

3- drag n drop answer

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2019 1