Discord
Login
Community
DARK THEME

What am I doing wrong?

Player = class
  
  constructor = function()
    this.x = 100
    this.y = 0
  end
  
  update = function( dt = 0 )
  end
  
  draw = function()
    screen.fillRect( this.x, this.y, 20, 40, "rgb( 255, 0, 0 )" ) //This line
  end
  
end

Warning: x is not defined, defaulting to zero, in file "lib/objects/player" at line 13, column 25

Warning: y is not defined, defaulting to zero, in file "lib/objects/player" at line 13, column 33

You probably call player.draw() before you create the object. Show full code.

MainGame = class extends Scene
  
  constructor = function()
    this.name = "main_game"
  end
  
  init = function()
    
    local Player = new Player()
    
  end
  
  update = function( dt = 0 )
  end
  
  draw = function()
    screen.fillRect( 0, 0, screen.width, screen.height, "rgb( 10, 10, 10 )")
    screen.drawMap( "test", 0, 0, screen.width, screen.height)
    
    Player.draw()
    
  end
  
end

Also the error contoninouly shows up sho its not a 1 time thing.

  init = function()
    
    this.player = new Player()
    
  end

  draw = function()
    screen.fillRect( 0, 0, screen.width, screen.height, "rgb( 10, 10, 10 )")
    screen.drawMap( "test", 0, 0, screen.width, screen.height)
    
    this.player.draw()
    
  end

1. local - a variable will only alive within the block in which it is defined. In this case it is only available in the init function.

2. You use a variable name identical to the class name. In this case you replace the class with a variable.

It doesnt work. Here is the current version:


MainGame = class extends Scene
  
  constructor = function()
    this.name = "main_game"
  end
  
  init = function()
    
    this.player = new Player()
    
  end
  
  update = function( dt = 0 )
    Player.update( dt )
  end
  
  draw = function()
    screen.fillRect( 0, 0, screen.width, screen.height, "rgb( 10, 10, 10 )")
    screen.drawMap( "test", 0, 0, screen.width, screen.height)
    
    this.player.draw()
  end
  
end

Player = class
  
  constructor = function()
    this.xPos = 0
    this.yPos = 0
  end
  
  update = function( dt = 0 )
    this.xPos += ( ( keyboard.RIGHT - keyboard.LEFT ) * 120 ) * dt
    this.yPos += ( ( keyboard.UP - keyboard.DOWN) * 120 ) * dt
  end
  
  draw = function()
    screen.fillRect( this.xPos, this.yPos, 20, 40, "rgb( 255, 0, 0 )" )
  end
  
end

And the errors are

Warning: xPos is not defined, defaulting to zero, in file "assets/objects/player" at line 10, column 16
Warning: yPos is not defined, defaulting to zero, in file "assets/objects/player" at line 11, column 16
Warning: xPos is not defined, defaulting to zero
Warning: yPos is not defined, defaulting to zero

https://microstudio.dev/i/MurdexStudio/smashlikegame2/

  update = function( dt = 0 )
    this.player.update( dt )
  end

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community