• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Random Timeline

Explorer ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

Hallo!

Ich habe eine nette kleine Flash-Animation in meinem Archiv die ich gerne für Animate/HTML5 nutzen würde.

Der Code war extrem simpel und bewirkte dass zufällig zwischen 4 Bildmarkierungen (f0 bis f3) gesprungen wurde.

Am Ende von ein paar Frames sitzt das Script und schickt den Player jeweils auf einen andere Markierung, wo er wieder läuft bis zum Script, usw..

Seit Stunden versuche ich das mit Javascript zu machen, leider erfolglos. So war es:

zufall = random(3);

if (Number(zufall) == 0) {

gotoAndPlay("f0");

}

if (Number(zufall) == 1) {

gotoAndPlay("f1");

}

if (Number(zufall) == 2) {

gotoAndPlay("f2");

}

if (Number(zufall) == 3) {

gotoAndPlay("f3");

}

Gibt es da keine einfache Lösung?

So geht es jedenfalls nicht (dass ich kein Programmierer bin sieht man vermutlich gleich 😉

var zufall;

zufall = Math.random() *4;

zufall2 = Math.ceil(zufall);

if (zufall2 == 1) {

gotoAndPlay("f0")

}

;

if (zufall2== 2) {

  gotoAndPlay("f1")

}

;

if (zufall2== 3) {

gotoAndPlay("f2")

}

;

if (zufall2== 4) {

gotoAndPlay("f3")

}

;

Al

Views

677

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
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 23, 2018 Jan 23, 2018

Hi.

Call this function one frame before every label:

this.randomFrame = function()

{

    this.gotoAndPlay("f" + parseInt(Math.random() * 4));

}

// call in this way: this.randomFrame();

Votes

Translate

Translate
Community Expert ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

Hi.

Call this function one frame before every label:

this.randomFrame = function()

{

    this.gotoAndPlay("f" + parseInt(Math.random() * 4));

}

// call in this way: this.randomFrame();

Votes

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
community guidelines
Explorer ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

thank you

Votes

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
community guidelines
Community Expert ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

You're welcome.

Votes

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
community guidelines
LEGEND ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

The parseInt() function is designed to accept a string and return a number​. It is NOT a function for converting floats to ints. The only reason your code isn't failing is because of JavaScript's automatic type conversion.

The correct function body would be:

this.gotoAndPlay("f" + Math.floor(Math.random() * 4));

Votes

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
community guidelines
Explorer ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

thank you too 🙂

Votes

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
community guidelines
Community Expert ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

"Definition and Usage

The parseInt() function parses a string and returns an integer."

JavaScript parseInt() Function

parseInt() - JavaScript | MDN

All official docs I could find and a lot of people out there say it is made to parse a string and return an integer.

Votes

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
community guidelines
LEGEND ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

Yes. That's what I just said.

Math.random() returns a number, not a string.

Votes

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
community guidelines
Community Expert ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

If it's not a string, parseInt() converts it to a string first and then, in the end, it returns a integer.

It works anyway.

Votes

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
community guidelines
LEGEND ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

JoãoCésar  wrote

It works anyway.

That attitude is exactly why there's so much fragile, buggy code in the world. If you want to use functions wrong in your own code, oh well, but anyone who posts wrong code on a public forum as the correct way to do things is spreading disinformation.

https://forums.adobe.com/people/Colin+Holgate  wrote

In AS3 terms a Number would mean that it could well be a floating point value. parseInt() does seem to really return an Integer, which you would need when constructing the label name.

There's no such thing as a true integer in JavaScript. There is only the "number" type for all numbers. The parseInt() function is so named because it's coded to parse integer strings. It still technically returns a float.

Votes

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
community guidelines
Community Expert ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

That attitude is exactly why there's so much fragile, buggy code in the world. If you want to use functions wrong in your own code, oh well, but anyone who posts wrong code on a public forum as the correct way to do things is spreading disinformation.

It's not misinformation. I'm not saying to anyone to use wrong code. Certainly Math.floor is the best option, but parseInt is not wrong as you say because it was designed to make this type of conversion.

Votes

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
community guidelines
LEGEND ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

No, parseInt() was not designed to make this type of conversion. According to the ECMAScript Language Specification, "The parseInt function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix."

Look, JavaScript was designed to be incredibly forgiving of programmers who don't know what they're doing. That's why it does automatic semicolon insertion. That's why if you try to access a property that doesn't exist it comes back with "undefined" instead of crashing. That's why if you try to use a variable that you haven't declared, it declares it for you. That's why variables are dynamically typed. And that's why it always, even in the built-in functions, does automatic type conversion.

So when someone passes a number to parseInt(), that's wrong. But JS makes it work anyway because that's how JS rolls. Even when it requires converting a number to a string just so it can convert it back to a number.

Now we're back to "It works, so who cares?" (in the same sense that using a pipe wrench as a hammer "works"). Well, the second point is that good code should have clarity of purpose. Whenever possible, it should be apparent what code is doing just by looking at it.

But this:

parseInt(Math.random() * 4)

Is not clear at all. You can see that a random number is being created and then converted to an integer, but it's not specified how that conversion is taking place. Is the number being rounded, floored, ceilinged, or something else? That detail can be the difference between code that works and code that (randomly) fails. Of course, it's not clear what's happening because converting a float to an int isn't what this function is for. It only does float-to-int conversion as a side effect of how the string-to-int conversion works (basically it scans the provided string until it encounters a non-digit, then truncates anything after that).

On the other hand, this:

Math.floor(Math.random() * 4)

Is perfectly clear. A random number is being generated, then mathematically floored. It's clear because what it's doing is right there in the function name, instead of some other function intended for a different purpose that you'd have to know how it behaves internally to figure out what exactly it's doing.

So what you need to do now is repost your solution using the correct conversion function, then get the OP or a moderator to mark that as the Correct Answer.

Votes

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
community guidelines
Community Expert ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

LATEST

"When the parseInt function is called, the following steps are taken:

  1. Let inputString be ToString(string)."

When I said "It works anyway" I didn't mean that I don't care. What I said is that it really does this conversion.

I wrote a possible solution for the OP. Never said it was the best.

You can ask him to mark your answer as the correct one.

Votes

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
community guidelines
LEGEND ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

In AS3 terms a Number would mean that it could well be a floating point value. parseInt() does seem to really return an Integer, which you would need when constructing the label name.

But I too would have used Math.floor() to get the integer.

Votes

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
community guidelines