Skip to main content
Participant
November 20, 2025
Answered

error create car stage canvas

  • November 20, 2025
  • 1 reply
  • 52 views

// Referência do palco
var stage = this;
var stage = new createjs.Stage("gameCanvas");
// Criando o Carro dinamicamente (instância da classe MovieClip)
var car = new createjs.Shape();
car.graphics.beginFill("green").drawRect(0, 0, 50, 50); // Um quadrado verde representando o carro
car.x = stage.canvas.width / 2; // Posição inicial no centro
car.y = stage.canvas.height - 1; // Posição no fundo da tela
stage.addChild(car); // Adiciona o carro ao palco

// Atualizando o palco
createjs.Ticker.framerate = 60;
createjs.Ticker.addEventListener("tick", stage);

// Movimentação do Carro
var carSpeed = 5;
stage.on("stagemousedown", function(evt) {
car.x += carSpeed; // Move o carro para a direita ao clicar
});

 

/////////error 

Uncaught TypeError: Cannot read properties of null (reading 'width')

 

please help me!

Correct answer JoãoCésar17023019

Hi.

 

One of the problems is that the local variable stage is shadowing the global variable stage (which is created in the published output) and receiving two different values in sequence.

Also there's a tick event that is not being used and it's not needed if the goal is to only move the car by click.

 

That being said, here is a suggestion:

var car = new createjs.Shape();

car.setBounds(0, 0, 50, 50);
car.size = car.getBounds();
car.graphics.beginFill("green").drawRect(0, 0, car.size.width, car.size.height);
car.speed = 5;
car.x = lib.properties.width / 2;
car.y = lib.properties.height - car.size.height;
stage.addChild(car);

this.stop(); // methods that change the display list may require the current timeline to be stopped

stage.on("stagemousedown", function()
{
	car.x += car.speed;
});

 

Note that I set bounds to the shape because CreateJS doesn't provide a way of calculating a shape's bound automatically, added a speed property to the car itself for a more objected oriented approach and left the stage's framerate to be set directly in the IDE of Animate.

I hope it helps.

Regards,
JC

1 reply

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
November 20, 2025

Hi.

 

One of the problems is that the local variable stage is shadowing the global variable stage (which is created in the published output) and receiving two different values in sequence.

Also there's a tick event that is not being used and it's not needed if the goal is to only move the car by click.

 

That being said, here is a suggestion:

var car = new createjs.Shape();

car.setBounds(0, 0, 50, 50);
car.size = car.getBounds();
car.graphics.beginFill("green").drawRect(0, 0, car.size.width, car.size.height);
car.speed = 5;
car.x = lib.properties.width / 2;
car.y = lib.properties.height - car.size.height;
stage.addChild(car);

this.stop(); // methods that change the display list may require the current timeline to be stopped

stage.on("stagemousedown", function()
{
	car.x += car.speed;
});

 

Note that I set bounds to the shape because CreateJS doesn't provide a way of calculating a shape's bound automatically, added a speed property to the car itself for a more objected oriented approach and left the stage's framerate to be set directly in the IDE of Animate.

I hope it helps.

Regards,
JC