Event Listener Question ?

Hi there

I created a button and a function that has Runtime:removeEventListeners(“enterFrame”,FUNCTION). It currently works fine but I wonder:

  • If there is any potential problem because the FUNCTION has already removed from Runtime ?

  • Is there a method which can let us know that if there is a FUNCTION existing in Runtime or in an obejct ?

Thank in advance
[import]uid: 111309 topic_id: 22193 reply_id: 322193[/import]

This should be fine.

The answer to the second question is no.

You can check if a function exists, provided you nil it out when you remove the listener, though :slight_smile: [import]uid: 52491 topic_id: 22193 reply_id: 88288[/import]

I have another question about display.newText function

I created this simple code

local \_currentTextBlock = "Aside: The fact that keys are references, and strings can be used, tells us something about Lua's internals. We must be referring to the same string when we get and set the value or table string keys wouldn't work! Strings are not duplicated in Lua internally, so when you reference a string with the same value you are referring to the same string. ";   
  
local intText = "aaaaaa"  
local displayCompareText = display.newText(abc, 10, 300, 300 ,0, native.systemFont, 20)  
displayCompareText:setTextColor(255,255,255)  
displayCompareText.text = \_currentTextBlock  

I realize that if you warp text in width = 300 and no restrict on height, the first line of text intText will be at x = 10, y = 300 but when you change to _currentTextBlock, the first line is at x = 10 , y may be at about 200 not at 300.

Another problem is if you set the iniText long enough to reach the second line

local intText = “aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”

Then you change to _currentTextBlock, the y location of the first line is about 200 and down to one line.

Please tell me how can I keep the first line at the original y ? [import]uid: 111309 topic_id: 22193 reply_id: 88312[/import]

I apologize but I’m not quite sure what you are asking - I see new text defined as a variable abc which I can’t see declared and I’m unsure why one would not restrict height.

If you could perhaps reword your questions? I understand you have two but I can’t quite tell exactly what you want to know. [import]uid: 52491 topic_id: 22193 reply_id: 88406[/import]

I am working on a simple RPG-style text chat. I am having some issues with control the line of the text so I didn’t restrict on height yet.

local HEIGHT = display.contentHeight  
local WIDTH = display.contentWidth  
   
local textBlocks = {   
 {"Kid", "Look, a robot!"},   
 {"Abe", "BLEEP-BLOOP. I am an Autonomous Botanical Engineer. You can call me ABE."},   
 {"Kid", "Hi Abe. Meet Frosty the Snowman."},   
 {"Frosty", "Happy Birthday!"},   
 {"Abe", "BEEP-BIP. Hello, Frosty."},   
 {"Abe", "Does this frog belong to you?"},   
 {"Frog", "Ribbit..."},   
 {"Kid", "No I've never seen him before. Aren't you cold frog?"},   
 {"Frog", "Ribbit..."}  
}  
  
local SPEAKER = 0;   
local TEXT = 1;   
local \_currentTextBlockIndex=0;   
local \_currentTextBlock = "Aside: The fact that keys are references, and strings can be used, tells us something about Lua's internals. We must be referring to the same string when we get and set the value or table string keys wouldn't work! Strings are not duplicated in Lua internally, so when you reference a string with the same value you are referring to the same string. ";   
local displayText = display.newText("Unknown:", 10, 300, 300 ,0, native.systemFont, 20)  
displayText:setTextColor(255, 255, 255)  
  
local abc = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"  
local displayCompareText = display.newText(abc, 350, 300, 300 ,0, native.systemFont, 20)  
displayCompareText:setTextColor(0,102,204)  
displayCompareText.text = \_currentTextBlock  
  
local function nextTextBlock()  
 print(\_currentTextBlockIndex)  
 local function addListener()  
 displayNextButton:removeEventListener("tap", nextTextBlock);   
 displayText.text = "";  
 \_currentTextBlockIndex = \_currentTextBlockIndex + 1;   
 \_currentTextBlock = textBlocks[\_currentTextBlockIndex][2];  
 --characterIcon.gotoAndStop(\_textBlocks[\_currentTextBlockIndex][SPEAKER]);  
 Runtime:addEventListener("enterFrame",updateText)  
 displayNextButton:addEventListener("tap", fillText);   
 end  
 timer.performWithDelay( 1, addListener )  
end  
  
function fillText(event)   
 print("filltext" .. \_currentTextBlockIndex)  
 displayText.text = \_currentTextBlock;   
 Runtime:removeEventListener("enterFrame", updateText);  
 if(\_currentTextBlockIndex \< #textBlocks) then  
 local function addListener()  
 displayNextButton:removeEventListener("tap", fillText);   
 displayNextButton:addEventListener("tap", nextTextBlock);   
 end  
 timer.performWithDelay( 1, addListener )  
 end  
end  
  
function updateText(e)   
 if(#displayText.text \< #\_currentTextBlock) then  
 displayText.text = string.sub(\_currentTextBlock , 0, #displayText.text+1);   
 else   
 --Runtime:removeEventListener("enterFrame", updateText);   
 fillText();   
 end  
end  
  
displayNextButton = display.newImage( "Button.png" )  
displayNextButton.x = 720  
displayNextButton.y = 450  
displayNextButton:addEventListener("tap", fillText);   
Runtime:addEventListener("enterFrame",updateText)  

How can I fix the first line of displayText.text at y = 300 ? [import]uid: 111309 topic_id: 22193 reply_id: 88542[/import]

When you tap to update the text you could state that the y=10, I believe that should reposition accordingly. [import]uid: 52491 topic_id: 22193 reply_id: 88583[/import]

Thank you, I add setReferencePoint and state the original y of the text. Everything works fine :slight_smile:

I have another problem that how can I reduce the radius of the display.newCircle() ?

local circle = display.newCircle( 300, 300, 50 )  
circle:setFillColor( 255, 255, 255)  
circle.strokeWidth = 7  
  
local hitCircle = display.newCircle( 300, 300, 200 )  
hitCircle:setFillColor( 185, 231, 40, 50 )  
hitCircle.strokeWidth = 7  
hitCircle:setStrokeColor(0,204,0)  
  
local function scoreCalculator(self, event)  
 print(self.width .. " - " .. self.height )  
 if (self.width \>= 50) then  
 self.width = self.width - 1  
 self.height = self.height - 1  
 --self:scale(0.995, 0.995)  
 end  
end  
  
hitCircle.enterFrame = scoreCalculator  
Runtime:addEventListener("enterFrame", hitCircle)  

It is true that if we scale the circle but the width and the height of the circle is still the same. My purpose is reducing the radius of the circle to a certain value then stop. If we calculate the width and height, whatever you do the circle keeps getting bigger.

Is there a simple way to reduce the radius of the circle ?
[import]uid: 111309 topic_id: 22193 reply_id: 88602[/import]

That’s pretty whacky, I believe is it because if you change the width and it doesn’t match the height it gets messed up with a circle.

You can scale, then manually give it the size you want after - might that be an option? Eg;

[lua]local circle = display.newCircle( 300, 300, 50 )
circle:setFillColor( 255, 255, 255)
circle.strokeWidth = 7

local hitCircle = display.newCircle( 300, 300, 200 )
hitCircle:setFillColor( 185, 231, 40, 50 )
hitCircle.strokeWidth = 7
hitCircle:setStrokeColor(0,204,0)

local function scoreCalculator(self, event)
if (self.xScale >= 0.28) then
self.xScale = self.xScale-0.01
self.yScale = self.yScale-0.01
else
Runtime:removeEventListener(“enterFrame”, hitCircle)
self.xScale, self.yScale = 1, 1
self.width, self.height= 100,100
end
end

hitCircle.enterFrame = scoreCalculator
Runtime:addEventListener(“enterFrame”, hitCircle)[/lua]

Something like that? [import]uid: 52491 topic_id: 22193 reply_id: 88657[/import]

Yeah, this is exactly what I want

Thank you so much :slight_smile: [import]uid: 111309 topic_id: 22193 reply_id: 88660[/import]

Fabulous! Good luck with your project :slight_smile: [import]uid: 52491 topic_id: 22193 reply_id: 88790[/import]