Problem understanding Pinball example

Good day,
this should be a fairly easy one as I’m still new at Lua.
In the pinball example (Pinball Madness, http://developer.anscamobile.com/code/pinball-madness), when you press anywhere on the screen, it pulls the spring and flips the flippers. So I thought, why not make the flippers and the spring that shoots the ball independent. I wanted to make it so you have to touch the spring to make it pull back and shoot, and touch anywhere else to flip the flippers.

So I checked out this section:

 local callSpring = function(event)  
 if(event.phase == "began") then   
 spring.pullBack()  
 pinballTableObjects.flipLeftFlipper()  
 pinballTableObjects.flipRightFlipper()   
 gameAudio.playFlipperSound()  
 end  
 if(event.phase == "ended") then   
 spring.shoot()  
 gameAudio.playPlungerSound()  
 pinballTableObjects.lowerLeftFlipper()  
 pinballTableObjects.lowerRightFlipper()  
 end  
 end  
  
 Runtime:addEventListener("touch", callSpring)  

In the last line there is “Runtime:addEventListener(“touch”, callSpring)” I thought “Runtime” means “everywhere”. So I made it "spring:addEventListener(“touch”,callSpring)
However now nothing happens, no flipping and no spring pulling.
I tried some other things but it doesn’t work, which I think is because of a lack of basic understanding of something.

Could somebody clear this up for me and tell me how I could make it so the flippers and the spring are able to be activated independently?

Thanks. [import]uid: 65258 topic_id: 15928 reply_id: 315928[/import]

In the sample spring in main.lua is no the spring image as you thought. its an external module.
see how it is declared…
[lua]local spring = require(“spring”)[/lua]
there is no handle for the spring image in main.lua. so you have to re code it a bit to suit ur need. [import]uid: 71210 topic_id: 15928 reply_id: 59002[/import]

Thanks to your reply I figured it out now. Another thing that I’m wondering is this: I’m testing it on the simulator and the ball quite often passes through the flippers and bumpers when going fast. Is that different when running on the device, or can you somehow fix this? Or is this just how the physics work in Corona and there is no way to fix this?

Edit: Oh while I’m at it, another small question: What is the code for switching true/false based on what it is right now? LIke if x=true then x=false and vice versa? I think in actionscript there was a simple code for that, where you could write that in 2 words or so. I can’t find it on google and I don’t remember what it was. [import]uid: 65258 topic_id: 15928 reply_id: 59092[/import]

its just because of calculation accuracy problems in physics. there are some settings which you can tweak to avoid this.

  1. add isBullet = true option for the ball
    http://developer.anscamobile.com/content/game-edition-physics-bodies#body.isBullet
  2. PositionIterations
    http://developer.anscamobile.com/content/game-edition-box2d-physics-engine#physics.setPositionIterations
  3. VelocityIterations
    http://developer.anscamobile.com/content/game-edition-box2d-physics-engine#physics.setVelocityIterations [import]uid: 71210 topic_id: 15928 reply_id: 59101[/import]

Thanks, that was helpful. Now I found another thing I don’t get. I wanted to try and make the flippers flip independently when you touch the left or right side of the screen. So I wanted to make two invisible “buttons” that are half the screen big each. I then tried to display an image for the button, but it won’t show up, no matter where I put the code.

local button = display.newImage( “button.png”)

That used to work with another project I fiddled around with, but here it doesn’t. I tried it in main.lua and pinballtableobjects.lua, neither seems to work. The image exists in the folder. Why is that, and how can I make different things show up?
[import]uid: 65258 topic_id: 15928 reply_id: 59305[/import]

Could anyone answer that last question for me? I know it’s stupid, but I can’t figure out what I’m doing wrong, since it seems to be according to documentation. [import]uid: 65258 topic_id: 15928 reply_id: 59515[/import]

make sure that no other images or group are above the image that you are doing. adding images and objects to the screen happens same was as a painter paints a picture what added last will be on top of other. so just check where you are putting the image.

also to add two buttons to individually control the flips, you can try adding it in the background image itself. just check which part the click is made.(whether event.x and event.y greater or less than center)

if you still have problem with this feel free to post your code to my email address renjith at technowand dot com. I will check and get back to you. :slight_smile: [import]uid: 71210 topic_id: 15928 reply_id: 59639[/import]

and for your question regarding toggling of the true and flase flag you may just use [lua] not [/lua]
eg:
[lua]local a = false
local b = not a
print(b)[/lua] [import]uid: 71210 topic_id: 15928 reply_id: 59650[/import]

Great, got it to work with the images. There is another strange thing though: The background and all the objects are one image, yet they all have collisions. Can’t find where that is specified though. I saw in the code that physicseditor was used. So was it used to specify which areas have collisions too? Would i have to use that if I want to change the table layout or move stuff around?

Also, regarding that left/right clicking (event.x greater or less than center), is there a code for easier writing of center? Or would I have to use the pixel count? Or something like contentwidth?

Thanks for all the help so far, this got me from completely stuck to understanding quite a lot of the code. [import]uid: 65258 topic_id: 15928 reply_id: 59714[/import]

Yes, The background is a single object with image part specified as physics body. you can do it manually or can use physics editor to do it easily. there will be one file in the folder with the custom shape specifications with lots of x,y coordinates written.

and for left right clicking you can set a collision listener on the image and on the listener function just check whether event.x is greater or less than display.contentCenterX.
event.x < display.contentCenterX means the click is on left side
event.x > display.contentCenterX means click is on right side
(make contentCenterX either left or right)

hope this helps… :slight_smile: [import]uid: 71210 topic_id: 15928 reply_id: 59730[/import]

Thank you! Yes, it actually helps an awful lot and I appreciate it very much.
I got the flipping to work now, depending if you touch left or right. I also understood the event began/ended thing in the process. :slight_smile:

Now, I still have problems with overlapping in physics collisions. Even got the ball stuck in the flippers a few times. I made sure the ball has the “isBullet” set to true. I also tried putting physics.setPositionIterations( 16 ) physics.setVelocityIterations(6) in the ball function, in the main.lua or in the flipper luas, but it doesn’t prevent it from happening. Sometimes the ball passes through the flippers, sometimes it gets stuck, sometimes it just overlaps.

Now, a pinball game would suffer greatly when the ball physics feel off and the player blames the game rather than himself for losing. :slight_smile: I googled about box2d myself a bit, but I can’t figure out why this doesn’t work. Maybe I’m putting the code in the wrong place?
For the physicseditor and background explanation, I haven’t found a file in the folder that just contains the background objects coordinates, but I found physics editor stuff in the luas of the flippers and bumpers. However I didn’t find the place where the table obstacles (like that slope) are defined, and as such I wouldn’t know which file to open with physicseditor to edit the background when I would place a new table sprite there. I haven’t bought physicseditor yet though, so maybe I will see it then.

For now the overlapping is giving me headaches. Is the number not high enough maybe?

Again, thanks for the great help, it makes getting the hang of it a whole lot easier.

Edit: Just downloaded the unregistered version of physicseditor. No file in the pinball folder can be opened with physicseditor, so I really have no clue how to edit that. [import]uid: 65258 topic_id: 15928 reply_id: 59811[/import]

you may not be able to find files which you can open with physics editor. the files are exported from physics editor and will be in .lua format.
see the files
bumperOne.lua
purpleBumper.lua
flipperLeft.lua
flipperRight.lua
pinball.lua

all the above files are exported from physics editor.

am not sure whether its possible to reverse engineer the lua files but you can easily create custome shapes using the images you have and physics editor.
:slight_smile: [import]uid: 71210 topic_id: 15928 reply_id: 59828[/import]

Wonderful, got it to work with physicseditor as well! A glorious feeling, I must say. :smiley:
For now the only question that remains is about the overlapping physics. I wrote about that in my previous reply, could you maybe tell me if I’m putting that in the wrong place, or if the numbers aren’t high enough, or what I could do? Should I turn down the speed from the original ball, to prevent that from happening? [import]uid: 65258 topic_id: 15928 reply_id: 59893[/import]