Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

object.x, object.y and require ()
Started by omg1024 Nov 10 2018 06:57 AM

13 replies to this topic
object.x object.y require
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

omg1024

[GLOBAL: userInfoPane.html]
omg1024
  • Observer

  • 7 posts
  • Corona SDK

Hello. I'm trying to build simple scene through modules. Working code looks like this:

		require ("bg1_static")
		require ("bg2_move")
		require ("bg3_move")

		require ("player_run")

		require ("bg4_move")

No errors allowed. But if i try to set position for "player_run" through function:

		require ("bg1_static")
		require ("bg2_move")
		require ("bg3_move")

		local player = require ("player_run")
		player.x = 100
		player.y = 100
		
		require ("bg4_move")

This leads to an error in the attach. I'm totally noob in Corona SDK, Lua, and programming as is, so i need to understand what kind of error this is - lack of Lua knowledge (minimal language basis for example), Corona SDK functions or both? Can someone explaind what exactelly wrong and give advice to learn direction which related with module builds? Thanks.

 

Attached Files

  • Attached File  123.png   108.04KB   0 downloads


[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,163 posts
  • Corona SDK

Can you share your player_run.lua file? I suspect you're not returning a table with the player object at the end. Make sure to use the blue <> button when sharing code.

 

Rob



[TOPIC: post.html]
#3

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,660 posts
  • Corona SDK

It is telling you that player is not a display object like you think it is, it is a boolean (true/false) value.

Therefore the player object is not a table and does not have an .x property or any other properties.

We can't tell you what's causing it without seeing player_run.lua

[TOPIC: post.html]
#4

omg1024

[GLOBAL: userInfoPane.html]
omg1024
  • Observer

  • 7 posts
  • Corona SDK

From this tutorial:
 
--Player run animation start
-----------------------------------------------------------------------------------------	
displayContentWidth = 1024
displayContentHeight = 768

local sheetData = {
		width=256,
		height=256,
		numFrames=4,
		sheetContentWidth=512,
		sheetContentHeight=512 }
		
local mySheet = graphics.newImageSheet ( "img/player/run/run_sheet.png", sheetData )

local sequenceData = {
		{ name = "normalRun", start=1, count=4, time=400 },
		{ name = "fastRun", frames={ 1, 2, 3, 4}, time=200 }
	}
	
	local animation = display.newSprite( mySheet, sequenceData )
	animation.x = displayContentWidth/2
	animation.y = displayContentHeight/2
	
	animation:setSequence( "normalRun" )
	--animation:setSequence( "fastRun" )
	animation:play()
-----------------------------------------------------------------------------------------	
--Player run animation end


[TOPIC: post.html]
#5

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,660 posts
  • Corona SDK

Ok, a Lua module must return something in order for it to be used by the scene that required it. Otherwise the code will just run and that's it - you've lost any references to the objects created. 

 

How does Corona know what 'player' is? Is it sheetData, mySheet, sequenceData, animation? 

 

This is a basic module:

 

 
local m = {}
 
m.myModuleVariable = 50
 
return m
 

 

Used as follows:

 

 
local theModule = require("myModule")
 
print (theModule.myModuleVariable)
theModule.myModuleVariable = 60
print (theModule.myModuleVariable)
 

 

So, in this case you have two choices. You can just return animation at the end of the player_run file if you don't care about accessing the rest of the stuff in there, or attach everything to m and return it (like I did with the myModuleVariable ), and then access your player sprite from that.



[TOPIC: post.html]
#6

omg1024

[GLOBAL: userInfoPane.html]
omg1024
  • Observer

  • 7 posts
  • Corona SDK

Thanks!



[TOPIC: post.html]
#7

omg1024

[GLOBAL: userInfoPane.html]
omg1024
  • Observer

  • 7 posts
  • Corona SDK

And again i don't understand what's wrong. This code looks fine (because Corona shown not any errors):

--bg4 start
-----------------------------------------------------------------------------------------	
	local imgW = display.contentWidth
	local imgH = display.contentHeight
	local scrollSpeed = -1.5
	
	--bg4 move
	local bg4a = display.newImageRect("img/bg/bg4.png", 1024, 768)
	bg4a.x = imgW*0.5;  bg4a.y= imgH/2.4;
	
	local bg4b = display.newImageRect("img/bg/bg4.png", 1024, 768)
	bg4b.x = imgW*1.5;  bg4b.y= imgH/2.4;
	
	local bg4c = display.newImageRect("img/bg/bg4.png", 1024, 768)
	bg4c.x = imgW*2.5; bg4c.y= imgH/2.4;
	
	local moveBool = true
	
	if moveBool == true then
	local function move( event )
	-- move backgrounds to the left by scrollSpeed, default is 2
	 bg4a.x = bg4a.x + scrollSpeed
	 bg4b.x = bg4b.x + scrollSpeed
	 bg4c.x = bg4c.x + scrollSpeed
	
	-- Set up listeners so when backgrounds hits a certain point off the screen,
	-- move the background to the right off screen
		
		if  bg4a.x < -512 then
			bg4a:translate( 2048, 0 )
		end
	
		if  bg4b.x < -512 then
			bg4b:translate( 2048, 0 )
		end
	
		if  bg4c.x< -512 then
			bg4c:translate( 2048, 0 )
		end

	end
	
	-- Create a runtime event to move backgrounds
	Runtime:addEventListener( "enterFrame", move )
	end

	if moveBool ==  false then
	local function move( event )
	-- move backgrounds to the left by scrollSpeed, default is 2
	 bg4a.x = bg4a.x + 0
	 bg4b.x = bg4b.x + 0
	 bg4c.x = bg4c.x + 0
	end
	
	-- Create a runtime event to move backgrounds
	Runtime:addEventListener( "enterFrame", move )
	end

	return moveBool
-----------------------------------------------------------------------------------------	
--bg4 end
 
 
Further i'm try to connect it into main.lua with false state for moveBool (in bg4_move.lua above that's true initially):
 
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

	display.setStatusBar(display.HiddenStatusBar) --disable phone ui
	
	local physics = require( "physics" )
	physics.start()

--connect modules start
-----------------------------------------------------------------------------------------
		require ("bg1_static")
		require ("bg2_move")
		require ("bg3_move")
		
		local player = require ("player_run")
		player.x = player.x + 512
		player.y = player.y + 510

		local bg4 = require ("bg4_move")
		bg4.moveBool = false
-----------------------------------------------------------------------------------------
--connect modules end

--player controls start
-----------------------------------------------------------------------------------------
		
	--local function PlrFastRun()
		--player:setSequence( "fastRun" )
		--player:play()
	--end
	--player:addEventListener( "tap", PlrFastRun )
-----------------------------------------------------------------------------------------
		
	local function PlrStopRun()
		player:setSequence( "stopRun" )
		player:play()
	end
	player:addEventListener( "tap", PlrStopRun )
	
-----------------------------------------------------------------------------------------
--player contros end

And get an error (pic in attach). What's i do wrong now?

 

Attached Files

  • Attached File  44.png   37.96KB   0 downloads


[TOPIC: post.html]
#8

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,660 posts
  • Corona SDK

You are returning moveBool from bg4_move. Therefore bg4 in main is not an image, it is a bool. You then try to access what amounts to moveBool.moveBool, which is obviously nil.

[TOPIC: post.html]
#9

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,660 posts
  • Corona SDK

You don't see any errors in bg4_move because you're not doing anything illegal. Corona says, 'hey, you want to create some images and then return a bool from this module? No problem."

Only when you try to do something with a bool that you think is an image does an error occur.

[TOPIC: post.html]
#10

omg1024

[GLOBAL: userInfoPane.html]
omg1024
  • Observer

  • 7 posts
  • Corona SDK

I change code, now it's not image or still? By the idea bool declares earler than any images and value also returns to bool. Or not? Error still shows anyway.

--bg4 start
-----------------------------------------------------------------------------------------	
	local imgW = display.contentWidth
	local imgH = display.contentHeight
	local scrollSpeed = -1.5	
	local moveBool = true
	
	if moveBool == true then
	scrollSpeed = -1.5
	else
	scrollSpeed = 0
	end
	
	--bg4 move
	local bg4a = display.newImageRect("img/bg/bg4.png", 1024, 768)
	bg4a.x = imgW*0.5;  bg4a.y= imgH/2.4;
	
	local bg4b = display.newImageRect("img/bg/bg4.png", 1024, 768)
	bg4b.x = imgW*1.5;  bg4b.y= imgH/2.4;
	
	local bg4c = display.newImageRect("img/bg/bg4.png", 1024, 768)
	bg4c.x = imgW*2.5; bg4c.y= imgH/2.4;
	
	local function move( event )
	-- move backgrounds to the left by scrollSpeed, default is 2
	 bg4a.x = bg4a.x + scrollSpeed
	 bg4b.x = bg4b.x + scrollSpeed
	 bg4c.x = bg4c.x + scrollSpeed
	
	-- Set up listeners so when backgrounds hits a certain point off the screen,
	-- move the background to the right off screen
		
		if  bg4a.x < -512 then
			bg4a:translate( 2048, 0 )
		end
	
		if  bg4b.x < -512 then
			bg4b:translate( 2048, 0 )
		end
	
		if  bg4c.x< -512 then
			bg4c:translate( 2048, 0 )
		end

	end

	-- Create a runtime event to move backgrounds
	Runtime:addEventListener( "enterFrame", move )
	
	return moveBool
-----------------------------------------------------------------------------------------	
--bg4 end


[TOPIC: post.html]
#11

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,660 posts
  • Corona SDK

In main.lua you have this code:

 

 
local bg4 = require ("bg4_move")
 

 

 

bg4 will be equal to whatever you return from bg4_move, just with a different name. Therefore:

 

- moveBool is a boolean set to true

- bg4 is a boolean set to true

- they are the same object with different names

 

Therefore, when you try to access bg4.moveBool, that does not exist, because moveBool.moveBool does not exist.

 

If you want to return everything from bg4_move.lua, and have access to it all, you need to do something like this:

 

 
local m = {}
 
m.moveBool = true
 
m.bg4a = .....
m.bg4b = ....
m.bg4c = ....
 
return m
 

 

Now, bg4.moveBool will exist, as will bg4.bg4a, bg4.bg4b and bg4.gb4c.

 

If you never need access to the images again, just leave them as local rather than attaching them to 'm'.



[TOPIC: post.html]
#12

omg1024

[GLOBAL: userInfoPane.html]
omg1024
  • Observer

  • 7 posts
  • Corona SDK

Now i have no any errors, but also no any affects when i try to set false for bg4:

--main.lua

local bg4 = require ("bg4_move")
bg4.moveBool = false
--bg4_move.lua

local bg4State = {}

local scrollSpeed = -1.5	
	
bg4State.moveBool = true
	
if bg4State.moveBool == true then
scrollSpeed = -1.5
else
scrollSpeed = 0
end

return bg4State

Probably something wrong in the conditions block? If that is, why i can set false in bg4_move.lua and this will work and not when i set it in the main.lua?



[TOPIC: post.html]
#13

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,660 posts
  • Corona SDK

Is that all the code? The code that checks whether moveBool is true or false only runs once. You need to either put it in an enterFrame listener that checks every frame, or create a function that is called manually each time you update moveBool.

[TOPIC: post.html]
#14

omg1024

[GLOBAL: userInfoPane.html]
omg1024
  • Observer

  • 7 posts
  • Corona SDK

Thanks again!




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: object.x, object.y, require