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

HTML5 Canvas Code Error

Community Beginner ,
Mar 10, 2023 Mar 10, 2023

I coded it so that when the button is clicked it goes to frame 2 and plays the animation, but when i test it goes to frame 4 instead? How can i fix.

This is my first time using HTML5 Canvas and just a quick demo so please excuse the bad quality.

TOPICS
Code , Error , How to , Product issue , Timeline
1.2K
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 ,
Mar 10, 2023 Mar 10, 2023

I coded the same thing just with a different button, so when I press the button the animation should go to frame 20 but instead, it goes to frame 22. Same problem!

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 ,
Mar 10, 2023 Mar 10, 2023

To clarify when I said "when the button is clicked it goes to frame 2" "it" is referring to the animation.

 

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 ,
Mar 10, 2023 Mar 10, 2023

Hi.

 

In the HTML5 Canvas document, the first frame is 0. So when you use gotoAndPlay(2), you're actually sending the timeline to the third frame. So you should use gotoAndPlay(1) to send to the second frame.

 

Also, the click event listener is being readded everytime that frame is revisited. Thus you should add a check to prevent it. For example:

var _this = this;

if (!_this.frame0Started) // frame0Started is just a custom property
{
	_this.button_1.on("click", function()
	{
		_this.gotoAndPlay(1);
	});

	_this.frame0Started = true;
}

 

I hope this helps.

 

Regards,

JC

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 ,
Mar 10, 2023 Mar 10, 2023

I copy and pasted the code you suggested and it went from being offset by 2 frames to 1 frame. I changed the start frame to 0 and it works well. But when I tried copying the code for another animation everything broke.

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 ,
Mar 11, 2023 Mar 11, 2023

create a new fla that allows you to simplify and test the problem you're seeing, so it's easier to understand what you're doing incorrectly.

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 ,
Mar 10, 2023 Mar 10, 2023

if you fail to account for the way canvas counts frames, you'll be off by 1 frame, not 2 frames.  are you sure it's off by 2, and not 1?

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 ,
Mar 12, 2023 Mar 12, 2023

Im sure

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 ,
Mar 12, 2023 Mar 12, 2023
quote

Im sure


By Roadbobek

 

meaning?

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 ,
Mar 13, 2023 Mar 13, 2023

I'm sure it's off my 2 frames.

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 ,
Mar 13, 2023 Mar 13, 2023

you created a test fla and something there is "off by 2 frames"?

 

if so, exactly what did you create in your test fla?

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 ,
Mar 15, 2023 Mar 15, 2023

It's just schoolwork, I attached 2 videos earlier.

 

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 ,
Mar 15, 2023 Mar 15, 2023
LATEST

trying to understand your error in anything other than a simple fla is often difficult. 

 

so again, create a new test fla with the bare minimum to display the problem you're having.  ie, a few keyframe with different content on stage that shows which frame is on stage and one of two button goto's.

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