Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

The problem with the generation of land under the player
Started by treshkobattle Apr 23 2018 09:19 PM

- - - - -
1 reply to this topic
lua function ground coronasdk
This topic has been archived. This means that you cannot reply to this topic.
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 1 posts
  • Corona SDK

I have a function called CreateGround, it creates a surface under the player and if it already exists on the site of the land, it moves along the x-axis to x + the width of the surface, but for some reason it refuses to work, I hope that you will help me.

Here's the code:

local function createGround ()
  local ground = display.newImageRect( mainGroup, objectSheet, 3, 390, 265 )
  ground.x = 195
  ground.y = yc+(yc*0.9)
  table.insert( groundTable, ground )
  for i = #groundTable, 1, -1 do
    local thisGround = groundTable[i]
    if (thisGround.x == (thisGround-1).x) then
      (thisGround-1).x = (thisGround-1).x + 390
    if (thisGround.x < 500 or thisGround.x > width+500) then
      display.remove( thisGround )
      table.remove( groundTable, i )

Error screen attached


Attached Files

[TOPIC: post.html]

Glitch Games

[GLOBAL: userInfoPane.html]
Glitch Games
  • Contributor

  • 597 posts
  • Corona SDK

It's this line that's causing the error:

 if (thisGround.x == (thisGround-1).x) then

You're trying to minus one from "thisGround" which is, presumably, a display object?


Did you want something more like this:

 if ( thisGround.x == ( groundTable[ i - 1 ] ).x ) then

If that's the case you'll also still want some safety checks in there to make sure there is an element in the groundTable at that index before you start accessing properties on it.