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

trying to add spacebar event listener

New Here ,
Aug 06, 2023 Aug 06, 2023

Copy link to clipboard

Copied

Hi there,

I'm trying to add an eventlistener to my HTML5 canvas project.  I've got a timeline which has the stop() comand at specific frames.  When the user presses the spacebar I'd like playback to continue from the current frame.

Here's what I've tried, but it doesn't work when published.

 

function onSpacebarPress(event) {
// Get the current frame number.
var currentFrame = stage.frameLabel;

// Play the timeline from the current frame.
timeline.play(currentFrame);
}

// Add the event listener.
stage.addEventListener("keydown", onSpacebarPress);

 

THanks in advance fo r any help!

TOPICS
How to

Views

897

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 , Aug 07, 2023 Aug 07, 2023

Hi.

 

The play method doesn't receive any arguments. Just call it like this:

var targetTL = this;

function onKeyDown(e)
{
	if (e.key === " ")
		targetTL.play();
}

if (!targetTL.frame0Started)
{
	window.addEventListener("keydown", onKeyDown);
	targetTL.frame0Started = true;
}

 

I hope this helps.

 

Regards,

JC

Votes

Translate

Translate
Community Expert ,
Aug 06, 2023 Aug 06, 2023

Copy link to clipboard

Copied

is that your code or pseudo-code?

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 ,
Aug 07, 2023 Aug 07, 2023

Copy link to clipboard

Copied

Hi.

 

The play method doesn't receive any arguments. Just call it like this:

var targetTL = this;

function onKeyDown(e)
{
	if (e.key === " ")
		targetTL.play();
}

if (!targetTL.frame0Started)
{
	window.addEventListener("keydown", onKeyDown);
	targetTL.frame0Started = true;
}

 

I hope this helps.

 

Regards,

JC

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
New Here ,
Aug 07, 2023 Aug 07, 2023

Copy link to clipboard

Copied

Thank you!  That works. 

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 ,
Aug 07, 2023 Aug 07, 2023

Copy link to clipboard

Copied

LATEST

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