0

I am making banner ads in AS3. Now I want to try converting them to HTML5 Canvas. My problem is the scripts. I dont know Java so I hope someone in here could help.

AS3: I insert this script on a frame and the animation stops for 5 seconds - and then continue playing.

sleep(5);
function sleep(sec) {
stop();
setTimeout(this.gotoAndPlay, sec*1000, this.currentFrame + 1);
}

How do I convert this to Javascript in my HTML5 Canvas? If possible...

  • You don't need Java, JavaScript and Java are as similar to each other as a cat is to a caterpillar. 2 different languages. One is a classic OO language, the other is actually a functional language – Elias Van Ootegem Feb 25 '14 at 08:50
  • Basically, in JS one would write: `stop(); setTimeout(playFuncReference, 5000);`. the `this` reference is bound _ad-hoc_, if a function is called in the global namespace, `this` will refer to the global object, if it's a method, `this` will point to the object of which the function is a method. – Elias Van Ootegem Feb 25 '14 at 09:30

1 Answers1

0

Javascript is very similar to actionscript, so something like this should work:

sleep(5);
function sleep(sec) {
       var self = this;
       setTimeout( function() {
           stop();
           self.gotoAndPlay(self.currentFrame + 1);
       }, sec*1000);
   );
}

Have in mind that gotoAndPlay should be declared and part of the parent object containing the sleep function, and stop should be declared in the same part as the sleep function to be accessible.

Goran.it
  • 5,991
  • 2
  • 23
  • 25
  • `this` will probably point to the global object here... AS3 and JS both start from the ECMA specs, sure, but they're far from identical – Elias Van Ootegem Feb 25 '14 at 08:51
  • http://stackoverflow.com/questions/2268276/what-are-the-key-differences-between-javascript-and-actionscript-3 – Goran.it Feb 25 '14 at 08:55
  • Yes, again: both ECMA, but _"almost identical"_? Absolutely _not_: type-safety, classical OO support, vector types, different std libs... just look at the code, `var foo = {foo: "bar"};` vs `var foo:Object = {foo: "bar"};`, `import`, `package`... heck: strong, static typing or the way `this` behaves. I'd say those are big differences – Elias Van Ootegem Feb 25 '14 at 09:10
  • Big difference would be this function written in python for example. If you compare actionscript and javascript versions in this case, would you say they differs a lot or ? – Goran.it Feb 25 '14 at 09:24
  • Yes, I would. Python just _looks_ different, because it doesn't use curly braces. Compare Java to C, and -syntax wise- you'd say they are quite similar, if you didn't know better. That doesn't make the languages similar. Not even close. In python: `a = "a string"\na = 123\n` is perfectly valid, as it is in JS. In AS3 `var a:String = "a string"\na = 123` is not. – Elias Van Ootegem Feb 25 '14 at 09:28
  • I wasn't talking about object / variable notations etc.. I was just saying something that has been said so many times over and over, that actionscript and javascript are similar.. do some google research about that. I won't discuss this any more as this is not the place to do that – Goran.it Feb 25 '14 at 09:32
  • Final example: Python -> `def myFunc (a, b): return (a + b)*myFunc.factor` then `myFunc.factor = 2` works fine, as it would in JS `function myFunc(a, b) { return (a + b)*myFunc.factor;} myFunc.factor = 2;` Using functions as objects, and assigning properties doesn't always work in AS3. Again: there _are_ similarities, but they are not, as you said _"almost identical"_ – Elias Van Ootegem Feb 25 '14 at 09:35
  • So in other words, you are commenting and forcing your statements only because of that "almost" in my response ? – Goran.it Feb 25 '14 at 09:44
  • I started off by saying they're far from identical. You responded with a link. I checked that link, and found my initial comment backed by the answers given there. You called them _almost identical_, I disagreed, and merely stated my reasons, responding to your counter-arguments is part of backing up _my_ claims. You're entitled to your views, and if they differ from mine, I'm the sort of person who likes to know why, so I can adapt my views. – Elias Van Ootegem Feb 25 '14 at 09:50