Small problem with the logic of the game

Hello everyone.

I have some difficulty with the logic of the game that I want to accomplish.

Simply I have a ship that the user can journeying with your finger.

On the ship there are elements.

These elements should be placed in the group of the ship in order to perform exactly the same movements.

Then I would like that if you experience situations after the touch, the elements move in a certain way.

I use the composer and I decided to create 3 modules:

  • Ship

  • Elements (a vector)

  • Check (for situations control after the touch module)

However, I do not know how to communicate with each other these 3 modules.

Should I create these 3 modules in my scene? And then how would I communicate?

Or would it be better to create the Ship and the Elements in Checks?

Or yet another best solution?

Would you give me a really big help, thanks.

I’m really stuck. :frowning:

No idea?

So you have the ship place in a group, and on the same group, you placed the elements to move on the same moment than the ship ?

Something like this :

[[ship.lua]] local g=display.newGroup() local Elements g:insert(Elements) local function ship(x,y,...) local ship=display.newImage(g,"ship.jpg") if (ship do this ...) then Elements[i] do that ... end return ship end 

Perhaps in creating some actions on ship’s object : ship:doThis() etc …

Give more informations plz

Thank you! finally help

Ok then the project is too big so I created the classes that I made in a very reduced and commented code so that I can explain.

I hope you understand.

--[Ship.lua] local M = {} function M.new( options ) local ship = display.newGroup() --check attribute to the ship ship.attribute1 = "value" ship.attribute2 = "value" ship.attribute3 = "value" local shipPiece = 10 ship.pieces = {} --loop for each piece of the group (thus the appearance) of the ship for i = 1, shipPiece do --I insert in the group ship the pieces that make up the ship ship.pieces[i] = display.newImageRect( ship, "filename.png", 80, 80 ) --here I would do more checks for the correct position but it is irrelevant ship:insert(ship.pieces[i]) end --touch event in ship group function ship:touch(event) --move the group of the ship --At the end i would like to call a method of Check end ship:addEventListener( "touch" ) return ship end return M --[Elements.lua] local M = {} function M.new( options ) local elements = {} --check attribute to the Elements elements.attribute1 = "value" elements.attribute2 = "value" elements.attribute3 = "value" local nElements = 25 for i = 1, nElements do elements[i] = {} --I create the element that I want to insert into the ship elements[i] = display.newImageRect( "elements.png", 10, 10 ) --here I would do more checks for the correct position but it is irrelevant --Here I would like to do something like ship:insert(elements[i]) end return Elements end return M --[Check.lua] local M = {} function M.new( options ) local check = {} --check attribute to the check check.attribute1 = "value" check.attribute2 = "value" check.attribute3 = "value" --all of the following functions they would partially amended with elements of checks on the ship owned --something like this: function check:function\_1() if(Ship.attribute1 == "something") then Elements[x].x = valueX Elements[y].x = valueY end end function check:function\_2() if(Ship.attribute2 == "something") then Elements[x]:removeSelf() Elements[x] = nil end end function check:function\_3() end return check end return M

Basically I want to understand what is the best way to communicate these three “objects” together in the composer scene

I have an idea, but you will delete the “check.lua” file.

I suggest to place the Elements table as a sub-table of ship (in "ship.elements) and replace the “check.lua” by using an custom dispatchEvent to listen the ship.

local function checkShip(event) local self=event.target local e=self.elements local t=event.attribute if(self.t.attribute1=="something")then e[x].x,e[y].y="valueX","valueY" end if(self.t.attribute2=="something")then e[x]:removeSelf();e[x]=nil end etc... return ??? (depend on what you fill in the if...then... below) end ship:addEventListener("check",checkShip) local event= { name="check",target=ship, attribute={attribute1="value",attribute2="value",...}, } ship:dispatchEvent(event)

Something like that !

Have a good luck.

Sincerely.

Yvan

 

 

The idea of putting the Elements inside the Ship was also coming to me, but I was not sure.

I think so lighthouse.

While as regards the Check are not very safe.

Always consider that I have simplified a lot.

Actually Check.lua has about 2000 lines of code and also recursive functions …

There some other “mechanism” to check?

Thank you very much for the tips are helping me a lot

I don’t know.

Me I will create a big function “check” to do the job.

Or if it’s on the check.lua file, call it when it’s needed.

[[other.lua]] local check=require("check")

I can’t help you much.

gl

Ok thanks for your time  :slight_smile:

I’m really stuck. :frowning:

No idea?

So you have the ship place in a group, and on the same group, you placed the elements to move on the same moment than the ship ?

Something like this :

[[ship.lua]] local g=display.newGroup() local Elements g:insert(Elements) local function ship(x,y,...) local ship=display.newImage(g,"ship.jpg") if (ship do this ...) then Elements[i] do that ... end return ship end 

Perhaps in creating some actions on ship’s object : ship:doThis() etc …

Give more informations plz

Thank you! finally help

Ok then the project is too big so I created the classes that I made in a very reduced and commented code so that I can explain.

I hope you understand.

--[Ship.lua] local M = {} function M.new( options ) local ship = display.newGroup() --check attribute to the ship ship.attribute1 = "value" ship.attribute2 = "value" ship.attribute3 = "value" local shipPiece = 10 ship.pieces = {} --loop for each piece of the group (thus the appearance) of the ship for i = 1, shipPiece do --I insert in the group ship the pieces that make up the ship ship.pieces[i] = display.newImageRect( ship, "filename.png", 80, 80 ) --here I would do more checks for the correct position but it is irrelevant ship:insert(ship.pieces[i]) end --touch event in ship group function ship:touch(event) --move the group of the ship --At the end i would like to call a method of Check end ship:addEventListener( "touch" ) return ship end return M --[Elements.lua] local M = {} function M.new( options ) local elements = {} --check attribute to the Elements elements.attribute1 = "value" elements.attribute2 = "value" elements.attribute3 = "value" local nElements = 25 for i = 1, nElements do elements[i] = {} --I create the element that I want to insert into the ship elements[i] = display.newImageRect( "elements.png", 10, 10 ) --here I would do more checks for the correct position but it is irrelevant --Here I would like to do something like ship:insert(elements[i]) end return Elements end return M --[Check.lua] local M = {} function M.new( options ) local check = {} --check attribute to the check check.attribute1 = "value" check.attribute2 = "value" check.attribute3 = "value" --all of the following functions they would partially amended with elements of checks on the ship owned --something like this: function check:function\_1() if(Ship.attribute1 == "something") then Elements[x].x = valueX Elements[y].x = valueY end end function check:function\_2() if(Ship.attribute2 == "something") then Elements[x]:removeSelf() Elements[x] = nil end end function check:function\_3() end return check end return M

Basically I want to understand what is the best way to communicate these three “objects” together in the composer scene

I have an idea, but you will delete the “check.lua” file.

I suggest to place the Elements table as a sub-table of ship (in "ship.elements) and replace the “check.lua” by using an custom dispatchEvent to listen the ship.

local function checkShip(event) local self=event.target local e=self.elements local t=event.attribute if(self.t.attribute1=="something")then e[x].x,e[y].y="valueX","valueY" end if(self.t.attribute2=="something")then e[x]:removeSelf();e[x]=nil end etc... return ??? (depend on what you fill in the if...then... below) end ship:addEventListener("check",checkShip) local event= { name="check",target=ship, attribute={attribute1="value",attribute2="value",...}, } ship:dispatchEvent(event)

Something like that !

Have a good luck.

Sincerely.

Yvan

 

 

The idea of putting the Elements inside the Ship was also coming to me, but I was not sure.

I think so lighthouse.

While as regards the Check are not very safe.

Always consider that I have simplified a lot.

Actually Check.lua has about 2000 lines of code and also recursive functions …

There some other “mechanism” to check?

Thank you very much for the tips are helping me a lot

I don’t know.

Me I will create a big function “check” to do the job.

Or if it’s on the check.lua file, call it when it’s needed.

[[other.lua]] local check=require("check")

I can’t help you much.

gl

Ok thanks for your time  :slight_smile: