Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

Multiple games with same script on timeline

Community Beginner ,
May 28, 2019 May 28, 2019

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?

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

correct answers 1 Correct answer

Community Expert , May 28, 2019 May 28, 2019

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.

Translate
Community Expert ,
May 28, 2019 May 28, 2019

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 28, 2019 May 28, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2019 May 28, 2019

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 28, 2019 May 28, 2019

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2019 May 28, 2019

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

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 29, 2019 May 29, 2019

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 : (

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 29, 2019 May 29, 2019
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 28, 2019 May 28, 2019

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

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