Attempts at modularization

Say you have one lua file containing all of the code for the user/player object. In a separate lua file you have all of the code for an enemy object. Is it possible to have the enemy ‘shoot a projectile’ at the player if the code is stored this way?

My current attempts reference the player object’s x/y for the ‘projectile,’ which returns nil when the code is separated.

I was wondering if anyone has an easy fix for this, or can point to an example/tutorial.

Thanks [import]uid: 102017 topic_id: 17887 reply_id: 317887[/import]

Yes it is possible. Take a look at these posts;

http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/
and
http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/

Peach :slight_smile: [import]uid: 52491 topic_id: 17887 reply_id: 68331[/import]

Thanks for the links, I have read them and attempted a simple version with four files that I think should work but it doesn’t.

I have provided the code below if anyone wants to read it.

First off, the terminal returns an error when I try to insert mainPlayer and newEnemy into the group. Second, as it sits the ‘bullet’ is spawned but doesn’t move. If I change the function with the transition from player.x/y to mainPlayer.x/y the terminal returns a nil value.

Thanks

[lua]–main.lua

local level1 = require (“level1”)

level1.new()

–level1.lua

module(…, package.seeall)

function new()
local player = require (“player”)
local enemy = require (“enemy”)

local levelGroup = display.newGroup()

local mainPlayer = player.spawnPlayer()
local newEnemy = enemy.spawnEnemy()

levelGroup:insert(mainPlayer)
levelGroup:insert(newEnemy)

return levelGroup
end

–player.lua

module(…, package.seeall)

function spawnPlayer()
local player = display.newCircle (120, 200, 25)
player:setFillColor(0,0,255)
end

–enemy.lua
module(…, package.seeall)

function spawnEnemy()
local enemy = display.newCircle (120, 400, 25)
enemy:setFillColor(255,0,0)

local function shoot (event)
local bullet = display.newCircle (enemy.x, enemy.y, 5)
transition.to (bullet, {time=500, x=player.x, y=player.y})
end
enemy:addEventListener(“tap”, shoot)
end[/lua] [import]uid: 102017 topic_id: 17887 reply_id: 68775[/import]

1 Like

For the groups, perhaps you could insert them both into a group in either their own spawn files OR in main.lua. The group has to exist they’re being added to and it can’t be seen during your level1 set up from main.

Peach :slight_smile: [import]uid: 52491 topic_id: 17887 reply_id: 68859[/import]

Thanks, with your help the group issue was solved.

So far I have ‘solved’ the other problem by placing any functions that reference the player.x and player.y in the level1.lua file. My skin crawls thinking about having to make changes to every level file if I want one small thing changed in these behaviors but it’s a step in the right direction.
[import]uid: 102017 topic_id: 17887 reply_id: 68965[/import]

Remove local from line 3 in player.lua, then in enemy.lua change line 9 to this;

[lua] transition.to (bullet, {time=500, x=player.player.x, y=player.player.y})[/lua]

Let me know how that goes :slight_smile:

Peach [import]uid: 52491 topic_id: 17887 reply_id: 69066[/import]

That works perfectly!

Thank you so much. [import]uid: 102017 topic_id: 17887 reply_id: 69192[/import]

No worries - thank you for providing sample code that used newCircle rather than images. (Plug and play code is MUCH easier to help people with.)

Peach :slight_smile: [import]uid: 52491 topic_id: 17887 reply_id: 69268[/import]