Skip to main content
fiddlefro
Participant
Aug 7, 2023 3:05 AM UTC
Answered

trying to add spacebar event listener

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!

This topic is closed to new replies. Start a new post to keep the conversation going.
JoãoCésar17023019
JoãoCésar17023019Correct answer
Community Expert
Aug 7, 2023 12:37 PM UTC

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

4 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
Aug 7, 2023 12:37 PM UTC

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

Translate
fiddlefro
fiddlefroAuthor
Participant
Aug 7, 2023 2:18 PM UTC

Thank you!  That works. 

Translate
JoãoCésar17023019
Community Expert
Community Expert
Aug 7, 2023 2:30 PM UTC

You're welcome!

Translate
kglad
Community Expert
Community Expert
Aug 7, 2023 3:11 AM UTC

is that your code or pseudo-code?

Translate