Hi community,
I am opening this thread to get some help (my previous thread was getting cluttered).
Do you know why isn't friend catching the message sent by enemy and how would you solve that?
-- main.lua
local mainGroup = Sprite.new()
stage:addChild(mainGroup)
-- ENEMY
local enemy = Enemy.new()
mainGroup:addChild(enemy)
enemy:setPosition(100, 50)
enemy:setColorTransform(1, 0, 0, 1)
-- FRIEND
local friend = Friend.new()
mainGroup:addChild(friend)
friend:setPosition(100, 600)
friend:setColorTransform(0, 1, 0, 1)
-- INTERACTIONS
-- ******************[ I want the friend to listen to the event ]*************************
friend:addEventListener("enemyIsClose", friend.enemyAlertFunction, friend)
-- ************************************************************************************
enemy:isClose() |
-- classes.lua
-- CHAR
Char = Core.class(Shape)
function Char:init()
self:setFillStyle(Shape.SOLID, 0xFFFFFF)
self:beginPath()
self:moveTo(0, 0)
self:lineTo(500, 0)
self:lineTo(500, 500)
self:lineTo(0, 500)
self:closePath()
self:endPath()
end
-- ENEMY
Enemy = Core.class(Char)
function Enemy:isClose()
local flag = true
if (flag) then
closerEvent = Event.new("enemyIsClose") --with the specific keyword for the event.
self:dispatchEvent(closerEvent)
print ("Send message to Friend : Enemy is close")
end
end
-- FRIEND
Friend = Core.class(Char)
function Friend:enemyAlertFunction(event)
print("Get message from Enemy : Enemy is close!")
end |
I can get "Send message to Friend : Enemy is close" to be printed but not "Get message from Enemy : Enemy is close!"
Why isn't friend catching the message sent by enemy?Thank you for your help
Comments
try
enemy:addEventListener("enemyIsClose", friend.enemyAlertFunction, friend)
https://sites.google.com/site/xraystudiogame
If the enemy is killed -> The friend won't be reacting to any other enemy.
I would like the friend to be able to listen to messages sent by any enemy, dynamically created.
So I thought I want to add the listener to the friend, and he would act on himself (run function).
So I don't want the enemy to act on the friend, I want the friend to listen and act.
Do you know how to do it?
Or maybe I'm mistaking the purpose of events and listeners?
I know what exactly you want is a broadcast event if available.
https://sites.google.com/site/xraystudiogame
Broadcast events
I have made a research and found one of your suggestions for custom events :
Suggestion for custom events.
The discussion happened on december 2011 and @atilim said : I'd be interested to know if this is achievable or if I have been looking for something that is not currently possible
There are several ways to do that, even if you do not have dispatch events.
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
I don't undestand how objects communicate? I can see how enemy can order friend to act, but that's all.
Please take a look at that image below :
Case 1. Enemy (A) tells Friend (B) to Fight -> Achievable and I know how to do it
Case 2. Enemy (A) tells Friend (B) that he is close. Friend (B) then decides himself that he should fight -> It seems that it is not achievable from what I could understand from the people that commented on my threads.
Case 3. A middle man (C) checks the distance constantly between objects (A and and then informs and giver orders to friend (B) "Enemy (A) is close, You should Fight!" -> Is it what you are suggesting?
But in that case it seems that "middle man" is doing everything himself, and he is not "intercepting" events....
... right?
Sorry for asking again something that probably looks evident, but I was thinking that the ability to set up Case 2 would be simple and achievable.
I thought that Case 2 was exactly what the purpose of "listening" was.
A couple of weeks back @atilim helped me out by posting the following recursive event dispatch idea:
The original thread is here:
http://www.giderosmobile.com/forum/discussion/comment/5755
Again, I apologise if this is not relevant to what you're looking for.
Best regards
It seems a bit complex for my level, I will need time to understand if this is relevant to my problem or not
By the way, I really like your multistroke gesture recognition system that enhanced the great work provided by @ar2rsawseen.
In the meantime, I hope I can get other people to suggest a solution.
Maybe something like this?