Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Red ball selected
Started by Jack G May 30 2019 12:48 PM

10 replies to this topic

Best Answer Rob Miracle , 05 June 2019 - 01:33 PM

In your options.lua file, you're drawing three balls on top of each other, but you never attach any touch or tap listeners to be able to select/toggle through which ball you want to use.

 

Then in your level1.lua you're explicitly just drawing a red ball.

 

There are multiple approaches to cycling through the balls on the options level, but I think I would just draw one ball, then add a touch or tap handler to it. When someone taps on it, use :setFillColor() to set the color you want to use and then give it a name attribute that you can pass to the next level.

 

Create only ball:

                local validBallColors = ["red", "pink", "blue"]
                local currentBallColor = 0
                local function ballTapHandler( event )
                    currentBallColor = currentBallColor + 1
                    if currentBallColor > 3 then
                        currentBallColor = 1
                    end
                    if validBallColors[currentBallColor] == "red" then
                         event.target:setFillColor(1, 0, 0)
                         composer.setVariable("ballColor","red")
                    elseif validBallColors[currentBallColor] == "pink" then
                         event.target:setFillColor(1, 0.75, 0.75)
                         composer.setVariable("ballColor","ping")
                    elseif validBallColors[currentBallColor] == "blue" then
                         event.target:setFillColor(0, 0, 1)
                         composer.setVariable("ballColor","blue")
                    end
                    return true
                end

		local ball = display.newImageRect( "redBall.png", 25, 25 ) 
		ball.x = display.contentCenterX - 110   
		ball.y = display.contentCenterY - 10
                ball:addEventListener("tap", ballTapHandler)

You don't need physics at all in this scene. I would remove all physics code from here.

 

Then in level1.lua where you create the ball:

local ballColor = composer.getVariable("ballColor)
local redBall = display.newImageRect( "redBall.png", 25, 25 ) 
redBall.x = 100   
redBall.y = 55
if ballColor == "red" then
     redBall:setFillColor(1, 0, 0)
else if ballColor = "blue" then
     redBall:setFillColor(0, 0, 1)
else
     redBall:setFillColor(1, 0.75, 0.75)
end
physics.addBody( redBall, "dynamic", { density=1.0, friction=0.5, bounce=0.1, radius=12 } ) 

Now I really wouldn't call it "redBall", but probably just ball since it could be any color.  This is of course untested code, but it should get you close. There are more efficient ways to deal with this, but for simplicity, this is probably the easiest without getting into understanding more about tables.

 

Rob

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Jack G

[GLOBAL: userInfoPane.html]
Jack G
  • Observer

  • 10 posts
  • Corona SDK

I'm hoping to make a little 2 level app just to prove an mid 50's guy like myself can actually make an app.  My question is what would be the code for selecting the Red ball in options so that it's used in the game.  Somebody asked this question and was given the answers of 1 and 2, which makes no sense to me.  

 

I hate putting it this way, but what is the dumbed down version of how to make the assign the Red ball?

 

 

Thanks

Attached Files



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,684 posts
  • Enterprise

Let me see if I understand your question correctly. You have an options screen where players can tap on a red, green or other colored ball to play with, then later on you want to create a ball that is that color that the player uses. Is that correct?

 

Assuming this is right, there are multiple ways to do this and a lot depends on how you want to do it.

 

One way, would be to have a single image that is gray  and when creating it, you can then set the fill color to what was selected. In your options screen, you can do something like:

local ballColor = ""
local function pickBallColor( event )
      if event.target.color == "red" then
           ballColor = "red"
      else if  event.target.color == "green" then
           ballColor = "green"
      else
           ballColor = "blue"
      end
      return true
end

local redBall = display.newImageRect("ball.png", 64, 64)
redBall:setFillColor(1, 0, 0)
redBall.color = "red"
redBall.x = 50
redBall.y = 100
redBall:addEventListener("tap", pickBallColor)

local greenBall = display.newImageRect("ball.png", 64, 64)
greenBall:setFillColor(0, 1, 0)
greenBall.color = "green"
greenBall.x = 50
greenBall.y = 150greenBall:addEventListener("tap", pickBallColor)

local blueBall = display.newImageRect("ball.png", 64, 64)
blueBall:setFillColor(0, 0, 1)
blueBall.color = red
blueBall.x = 50
blueBall.y = 200
blueBall:addEventListener("tap", pickBallColor)

Now when you go to create the game ball:

local gameBall = display.newImageRect("ball.png", 64, 64)
if ballColor == "red" then
    gameBall:setFillColor( 1, 0, 0 )
else if ballColor == "green" then
    gameBall:setFillColor( 0, 1, 0 )
else
    gameBall:setFillColor( 0, 0, 1 )
end
gameBall.x = 50
gameBall.y = 50

Now this code likely isn't copy and pastable because I don't know how your code is structured. I don't know if you're using scenes, so passing the value of ballColor around may have to be handled differently.

 

You can also simply hide the other two balls that were not tapped on and not much with testing which color is in use. But you have to have the ball available in all scenes which will take a little more setup. (Look at the "GoodBye Globals" tutorial for more information)

 

And then add your listeners to the ball that you kept.

 

Rob



[TOPIC: post.html]
#3

Jack G

[GLOBAL: userInfoPane.html]
Jack G
  • Observer

  • 10 posts
  • Corona SDK

Rob the code you gave didn't work, these snips are from a game called Bubble Ball.  It's a very simple game which is perfect for me; I'm trying to make a little practice game similar to this game with 2 or 3 levels, just for learning purposes.

 

I'm using png's, not fill colors.  Just like this game, my game has the redBall.png which has to be tapped once the ball is selected that's the color that will be used in the game.

 

I can't figure out how to select the png and make it be the ball that the player will be using.

 

I really wish I had learned this a long time ago.

 

 

Thanks again

Attached Files



[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,684 posts
  • Enterprise

I said my code likely wouldn't work. Without seeing your  and how it's structured, I can really only provide generic code examples. Do you have a link to the project?  Can you post more of your code that's not working. Please use copy/paste to share code and not screen shots.



[TOPIC: post.html]
#5

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,085 posts
  • Corona SDK

You could gray scale your pngs and then use setFillColor().  For best results after gray scaling lighten the png a bit. 



[TOPIC: post.html]
#6

Jack G

[GLOBAL: userInfoPane.html]
Jack G
  • Observer

  • 10 posts
  • Corona SDK

Sorry Rob I didn't mean to insult you or your code, I know I'm the problem.  I've got my tabs set up from left to right, starting with 

 

build.settings

settings =
{
	orientation =
	{
		-- Supported values for orientation:
		-- portrait, portraitUpsideDown, landscapeLeft, landscapeRight
		default = "landscapeRight",
		supported = { "landscapeRight", },
	},

	--
	-- Android section
	--
	android =
	{
		usesPermissions =
		{
			"android.permission.INTERNET",
		},
	},

	--
	-- iOS section
	--
	iphone =
	{
		--xcassets = "Images.xcassets", -- Removed string ---
		plist =
		{
			UIStatusBarHidden = false,
			UILaunchStoryboardName = "LaunchScreen",
		},
	},

	--
	-- Plugins section
	--
	plugins =
	{

	},

	--
	-- Project section
	--
	excludeFiles =
	{
		-- Exclude unnecessary files for each platform
		all = { "Icon.png", "Icon-*dpi.png", "Images.xcassets", },
		android = { "LaunchScreen.storyboardc", },
	},
}


config

application =
{
	content =
	{
		width = 320,
		height = 480, 
		scale = "letterbox",
		fps = 60,
		
		--[[
		imageSuffix =
		{
			    ["@2x"] = 2,
			    ["@4x"] = 4,
		},
		--]]
	},
}

main

display.setStatusBar( display.HiddenStatusBar )


-- include the Corona "composer" module --- 
local composer = require "composer"


-- load menu screen --- 
composer.gotoScene( "menu" )

menu

local composer = require( "composer" )
local scene = composer.newScene()

-- include Corona's "widget" library --- 
local widget = require "widget"

-- include Corona's "physics" library
local physics = require "physics"

-----------------------------------------------------------------------------------------

local playBtn1 	

	local function onPlayBtn1Release() 	-- 'onRelease' event listener for playBtn --- 
	
				composer.gotoScene( "level1", "fade", 500 ) 

			return true	 -- indicates successful touch --- 
	end

-----------------------------------------------------------------------------------------

local playBtn2 	

	local function onPlayBtn2Release() 	-- 'onRelease' event listener for playBtn --- 
	
				composer.gotoScene( "options", "fade", 500 )  

			return true	 -- indicates successful touch --- 
	end

-----------------------------------------------------------------------------------------

function scene:create( event )
	local sceneGroup = self.view

	-- display a background image --- 
	local background = display.newImageRect( "background.png", display.actualContentWidth, display.actualContentHeight )
	background.anchorX = 0
	background.anchorY = 0
	background.x = 0 + display.screenOriginX 
	background.y = 0 + display.screenOriginY 

	-- create/position logo/title image on upper-half of the screen --- 
	local titleLogo = display.newImageRect( "logo.png", 300, 40 )  -- changed logo png ---
	titleLogo.x = display.contentCenterX
	titleLogo.y = 40
	
-----------------------------------------------------------------------------------------

	playBtn1 = widget.newButton{
		label="Levels",
		labelColor = { default={ 0, 0, 0 }, over={128} },
		defaultFile="buttonDefault1.png",     
		overFile="buttonOver1.png",			
		width=100, 
		height=30,
		onRelease = onPlayBtn1Release	-- event listener function --- 
	}
	playBtn1.x = display.contentCenterX
	playBtn1.y = display.contentHeight - 190

-----------------------------------------------------------------------------------------

	playBtn2 = widget.newButton{
		label="Options",
		labelColor = { default={ 0, 0, 0 }, over={128} },
		defaultFile="buttonDefault2.png",	
		overFile="buttonOver2.png",			
		width=100, 
		height=30,
		onRelease = onPlayBtn2Release	-- event listener function --- 
	}
	playBtn2.x = display.contentCenterX
	playBtn2.y = display.contentCenterY + 20

-----------------------------------------------------------------------------------------

	-- all display objects must be inserted into group --- 
	sceneGroup:insert( background )
	sceneGroup:insert( titleLogo )
	sceneGroup:insert( playBtn1 )
	sceneGroup:insert( playBtn2 )
end

-----------------------------------------------------------------------------------------

function scene:show( event )
	local sceneGroup = self.view
	local phase = event.phase
	
	if phase == "will" then

		-- Called when the scene is still off screen and is about to move on screen --- 
	elseif phase == "did" then
		-- Called when the scene is now on screen --- 
		-- 
		-- INSERT code here to make the scene come alive --- 
		-- e.g. start timers, begin animation, play audio, etc. --- 
	end	
end

-----------------------------------------------------------------------------------------

function scene:hide( event )
	local sceneGroup = self.view
	local phase = event.phase
	
	if event.phase == "will" then
		-- Called when the scene is on screen and is about to move off screen --- 
		--
		-- INSERT code here to pause the scene --- 
		-- e.g. stop timers, stop animation, unload sounds, etc.) --- 
	elseif phase == "did" then
		-- Called when the scene is now off screen --- 
	end	
end

-----------------------------------------------------------------------------------------

function scene:destroy( event )
	local sceneGroup = self.view
	
	-- Called prior to the removal of scene's "view" (sceneGroup) --- 
	-- 
	-- INSERT code here to cleanup the scene ---
	-- e.g. remove display objects, remove touch listeners, save state, etc. ---
	
	if playBtn1 then
		playBtn1:removeSelf()	-- widgets must be manually removed ---
		playBtn1 = nil	
	end
end

-----------------------------------------------------------------------------------------

-- Listener setup --- 
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-----------------------------------------------------------------------------------------

return scene

options

local composer = require( "composer" )
local scene = composer.newScene()

-- include Corona's "widget" library 
local widget = require "widget"

-- include Corona's "physics" library
local physics = require "physics"

-----------------------------------------------------------------------------------------

-- forward declarations and other locals
local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX

function scene:create( event )

	-- Called when the scene's view does not exist.
	-- 
	-- INSERT code here to initialize the scene
	-- e.g. add display objects to 'sceneGroup', add touch listeners, etc.

	local sceneGroup = self.view

	-- We need physics started to add bodies, but we don't want the simulation
	-- running until the scene is on the screen.
	physics.start()
	physics.pause()

-----------------------------------------------------------------------------------------


	local function onMenuTouch(event)
			if(event.phase == "ended") then
				composer.gotoScene("menu", "fade") 
			end
		
	end

-----------------------------------------------------------------------------------------

-- display a options background image --- 
local background = display.newImageRect( "background.png", display.actualContentWidth, display.actualContentHeight ) 
background.anchorX = 0 
background.anchorY = 0 
background.x = 0 + display.screenOriginX 
background.y = 0 + display.screenOriginY 

-----------------------------------------------------------------------------------------

--Function to handle button events ---
local function handleButtonEvent( event )
 
    if ( "ended" == event.phase ) then
        print( "Menu Button was pressed and released" )
    end
end
 
local button1 = widget.newButton(
    {
      label="Menu",
	  labelColor = { default={128}, over={128} },
        width = 55,
        height = 20,
        defaultFile = "menuBtnDefault.png",
        overFile = "menuBtnOver.png",
        onEvent = handleButtonEvent
                                           
    }
)
 
-- Center the button ---
button1.x = display.contentCenterX -250
button1.y = display.contentCenterY -140
button1:addEventListener("touch", onMenuTouch)

-----------------------------------------------------------------------------------------

local textObjColor = display.newText("Tap to select", 150, 20, native.systemFont, 20)
textObjColor:setFillColor(0, 0, 0)
textObjColor.x = display.contentCenterX - 160
textObjColor.y = display.contentCenterY - 40

-----------------------------------------------------------------------------------------

		local pinkBall = display.newImageRect( "pinkBall.png", 25, 25 ) 
		pinkBall.x = display.contentCenterX - 190   
		pinkBall.y = display.contentCenterY - 10
		physics.addBody( pinkBall, "static", { density=1.0, radius=12 } )

		local blueBall = display.newImageRect( "blueBall.png", 25, 25 ) 
		blueBall.x = display.contentCenterX - 150   
		blueBall.y = display.contentCenterY - 10
		physics.addBody( blueBall, "static", { density=1.0, radius=12 } ) 

		local redBall = display.newImageRect( "redBall.png", 25, 25 ) 
		redBall.x = display.contentCenterX - 110   
		redBall.y = display.contentCenterY - 10
		physics.addBody( redBall, "static", { density=1.0, radius=12 } )

-----------------------------------------------------------------------------------------

	sceneGroup:insert( background )
	sceneGroup:insert( textObjColor )  
	sceneGroup:insert( button1 ) 
	sceneGroup:insert( pinkBall ) 
	sceneGroup:insert( blueBall ) 
	sceneGroup:insert( redBall )
end

-----------------------------------------------------------------------------------------

function scene:show( event )
	local sceneGroup = self.view
	local phase = event.phase
	
	if phase == "will" then
		-- Called when the scene is still off screen and is about to move on screen
	elseif phase == "did" then
		-- Called when the scene is now on screen
		-- 
		-- INSERT code here to make the scene come alive
		-- e.g. start timers, begin animation, play audio, etc.
		physics.start()
	end
end

-----------------------------------------------------------------------------------------

function scene:hide( event )
	local sceneGroup = self.view
	
	local phase = event.phase
	
	if event.phase == "will" then
		-- Called when the scene is on screen and is about to move off screen
		--
		-- INSERT code here to pause the scene
		-- e.g. stop timers, stop animation, unload sounds, etc.)
		physics.stop()
	elseif phase == "did" then
		-- Called when the scene is now off screen
	end	
	
end

-----------------------------------------------------------------------------------------

function scene:destroy( event )

	-- Called prior to the removal of scene's "view" (sceneGroup)
	-- 
	-- INSERT code here to cleanup the scene
	-- e.g. remove display objects, remove touch listeners, save state, etc.
	local sceneGroup = self.view
	
	package.loaded[physics] = nil
	physics = nil
end

-----------------------------------------------------------------------------------------

-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-----------------------------------------------------------------------------------------

return scene 

level1

local composer = require( "composer" )
local scene = composer.newScene()

-- include Corona's "widget" library 
local widget = require "widget"

-- include Corona's "physics" library
local physics = require "physics"
physics.start()
physics.setGravity( 0, 9.8 )

-----------------------------------------------------------------------------------------

-- forward declarations and other locals
local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX

function scene:create( event )

	local sceneGroup = self.view

	-- We need physics started to add bodies, but we don't want the simulaton
	-- running until the scene is on the screen.
	physics.start()
	physics.pause()

-----------------------------------------------------------------------------------------

	local function onMenuTouch(event)
		
		if(event.phase == "ended") then
			composer.gotoScene("menu", "fade")
		end

	end

-----------------------------------------------------------------------------------------

	-- display a background image --- 
	local background = display.newImageRect( "background.png", display.actualContentWidth, display.actualContentHeight )
	background.anchorX = 0
	background.anchorY = 0
	background.x = 0 + display.screenOriginX 
	background.y = 0 + display.screenOriginY

-----------------------------------------------------------------------------------------

--Function to handle button events ---
local function handleButtonEvent( event )
 
    if ( "ended" == event.phase ) then
        print( "Menu Button was pressed and released" )
    end
end
 
local button1 = widget.newButton(
    {
      label="Menu",
	  labelColor = { default={128}, over={128} },
        width = 55,
        height = 20,
        defaultFile = "menuBtnDefault.png",
        overFile = "menuBtnOver.png",
        onEvent = handleButtonEvent
                                           
    }
)
 
-- Center the button ---
button1.x = display.contentCenterX -250
button1.y = display.contentCenterY -140
button1:addEventListener("touch", onMenuTouch)

-----------------------------------------------------------------------------------------

								
local button2 = widget.newButton(
    {
      label="Replay", -- Replay Button doesn't work, don't know that code either --
	  labelColor = { default={128}, over={128} },
        width = 60,
        height = 20,
        defaultFile = "replayBtnDefault.png",
        overFile = "replayBtnOver.png",
        onEvent = handleButtonEvent
                                
    }
)
 
-- Center the button ---
button2.x = display.contentCenterX +250
button2.y = display.contentCenterY -140

-----------------------------------------------------------------------------------------

local redBall = display.newImageRect( "redBall.png", 25, 25 ) 
redBall.x = 100   
redBall.y = 55
physics.addBody( redBall, "dynamic", { density=1.0, friction=0.5, bounce=0.1, radius=12 } )  

-----------------------------------------------------------------------------------------

local blackBlock = display.newImageRect( "blackBlock.png", 100, 40 )
blackBlock.x = 330
blackBlock.y = 190
physics.addBody( blackBlock, "static", { bounce=0.2 } ) 

-----------------------------------------------------------------------------------------
	
local greenBar1 = display.newImageRect("greenBar1.png", 70, 10) 
greenBar1.x = 98 
greenBar1.y = 88
greenBar1.rotation=0 
physics.addBody( greenBar1, "kinematic", { density=1.0, friction=0.3, bounce=1 } ) 

	local greenBar2 = display.newImageRect("greenBar2.png", 70, 10) 
	greenBar2.x = 200 
	greenBar2.y = 140
	greenBar2.rotation=0 
	physics.addBody( greenBar2, "kinematic", { density=1.0, friction=0.3, bounce=1 } ) 

-----------------------------------------------------------------------------------------
	
	sceneGroup:insert( background )
	sceneGroup:insert( button1 ) 
	sceneGroup:insert( button2 ) 
	sceneGroup:insert( redBall )
	sceneGroup:insert( blackBlock ) 
	sceneGroup:insert( greenBar1 ) 
	sceneGroup:insert( greenBar2 ) 

------------------------------------------------------------------------------------------

local function doRectTouch( event )

        local touchedObject = event.target

            if event.phase == "began" then
                display.getCurrentStage():setFocus( touchedObject )  -- sets focus on greenBar --
                
                touchedObject.previousX = touchedObject.x
                touchedObject.previousY = touchedObject.y
                print("greenBar1 touched")


            elseif event.phase == "moved" then 
              
                touchedObject.y = ( event.y - event.yStart )  + touchedObject.previousY
                print("greenBar1 moved")


            elseif event.phase == "ended" or event.phase == "cancelled" then

                display.getCurrentStage():setFocus(nil) -- removes focus from greenBar --
                print("greenBar1 released")

    end
    return true
end 

greenBar1:addEventListener( "touch", doRectTouch ) 

-----------------------------------------------------------------------------------------

	local function greenBar1Tapped( event )
		      transition.to( greenBar1, { rotation=20, delta= true } ) 
		end

			greenBar1:addEventListener( "tap", greenBar1Tapped )
		
end

-----------------------------------------------------------------------------------------

function scene:show( event )
	local sceneGroup = self.view
	local phase = event.phase
	
	if phase == "will" then
		-- Called when the scene is still off screen and is about to move on screen
	elseif phase == "did" then
		-- Called when the scene is now on screen
		-- 
		-- INSERT code here to make the scene come alive
		-- e.g. start timers, begin animation, play audio, etc.
		physics.start()
	end
end

-----------------------------------------------------------------------------------------

function scene:hide( event )
	local sceneGroup = self.view
	
	local phase = event.phase
	
	if event.phase == "will" then
		-- Called when the scene is on screen and is about to move off screen
		--
		-- INSERT code here to pause the scene
		-- e.g. stop timers, stop animation, unload sounds, etc.)
		physics.stop()
	elseif phase == "did" then
		-- Called when the scene is now off screen
	end	
	
end

-----------------------------------------------------------------------------------------

function scene:destroy( event )

	-- Called prior to the removal of scene's "view" (sceneGroup)
	-- 
	-- INSERT code here to cleanup the scene
	-- e.g. remove display objects, remove touch listeners, save state, etc.
	local sceneGroup = self.view
	
	package.loaded[physics] = nil
	physics = nil
end

-----------------------------------------------------------------------------------------

-- Listener setup --
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-----------------------------------------------------------------------------------------

return scene

Well that's all I've got right now; I have the Red ball code entered in level1, you know why. 

 

Thank you for patience

 

 

 



[TOPIC: post.html]
#7

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,684 posts
  • Enterprise

  Best Answer

In your options.lua file, you're drawing three balls on top of each other, but you never attach any touch or tap listeners to be able to select/toggle through which ball you want to use.

 

Then in your level1.lua you're explicitly just drawing a red ball.

 

There are multiple approaches to cycling through the balls on the options level, but I think I would just draw one ball, then add a touch or tap handler to it. When someone taps on it, use :setFillColor() to set the color you want to use and then give it a name attribute that you can pass to the next level.

 

Create only ball:

                local validBallColors = ["red", "pink", "blue"]
                local currentBallColor = 0
                local function ballTapHandler( event )
                    currentBallColor = currentBallColor + 1
                    if currentBallColor > 3 then
                        currentBallColor = 1
                    end
                    if validBallColors[currentBallColor] == "red" then
                         event.target:setFillColor(1, 0, 0)
                         composer.setVariable("ballColor","red")
                    elseif validBallColors[currentBallColor] == "pink" then
                         event.target:setFillColor(1, 0.75, 0.75)
                         composer.setVariable("ballColor","ping")
                    elseif validBallColors[currentBallColor] == "blue" then
                         event.target:setFillColor(0, 0, 1)
                         composer.setVariable("ballColor","blue")
                    end
                    return true
                end

		local ball = display.newImageRect( "redBall.png", 25, 25 ) 
		ball.x = display.contentCenterX - 110   
		ball.y = display.contentCenterY - 10
                ball:addEventListener("tap", ballTapHandler)

You don't need physics at all in this scene. I would remove all physics code from here.

 

Then in level1.lua where you create the ball:

local ballColor = composer.getVariable("ballColor)
local redBall = display.newImageRect( "redBall.png", 25, 25 ) 
redBall.x = 100   
redBall.y = 55
if ballColor == "red" then
     redBall:setFillColor(1, 0, 0)
else if ballColor = "blue" then
     redBall:setFillColor(0, 0, 1)
else
     redBall:setFillColor(1, 0.75, 0.75)
end
physics.addBody( redBall, "dynamic", { density=1.0, friction=0.5, bounce=0.1, radius=12 } ) 

Now I really wouldn't call it "redBall", but probably just ball since it could be any color.  This is of course untested code, but it should get you close. There are more efficient ways to deal with this, but for simplicity, this is probably the easiest without getting into understanding more about tables.

 

Rob


  • Jack G likes this

[TOPIC: post.html]
#8

Jack G

[GLOBAL: userInfoPane.html]
Jack G
  • Observer

  • 10 posts
  • Corona SDK

Rob it works!!  Everything works great, except for one problem the balls won't change they stay Red and Navy Blue. I checked the corona labs and other sites to be certain I was enter the code correctly, but they always stay the same Red and Navy Blue.

 

options

local composer = require( "composer" )
local scene = composer.newScene()

-- include Corona's "widget" library 
local widget = require "widget"

-----------------------------------------------------------------------------------------

-- forward declarations and other locals
local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX

function scene:create( event )

	-- Called when the scene's view does not exist.
	-- 
	-- INSERT code here to initialize the scene
	-- e.g. add display objects to 'sceneGroup', add touch listeners, etc.

	local sceneGroup = self.view

	-- We need physics started to add bodies, but we don't want the simulation
	-- running until the scene is on the screen.
	physics.start()
	physics.pause()

-----------------------------------------------------------------------------------------

	local function onMenuTouch(event)
			if(event.phase == "ended") then
				composer.gotoScene("menu", "fade") 
			end
		
	end

-----------------------------------------------------------------------------------------

-- display a options background image --- 
local background = display.newImageRect( "background.png", display.actualContentWidth, display.actualContentHeight ) 
background.anchorX = 0 
background.anchorY = 0 
background.x = 0 + display.screenOriginX 
background.y = 0 + display.screenOriginY 

-----------------------------------------------------------------------------------------

--Function to handle button events ---
local function handleButtonEvent( event )
 
    if ( "ended" == event.phase ) then
        print( "Menu Button was pressed and released" )
    end
end
 
local button1 = widget.newButton(
    {
      label="Menu",
	  labelColor = { default={128}, over={128} },
        width = 55,
        height = 20,
        defaultFile = "menuBtnDefault.png",
        overFile = "menuBtnOver.png",
        onEvent = handleButtonEvent
                                           
    }
)
 
-- Center the button ---
button1.x = display.contentCenterX -250
button1.y = display.contentCenterY -140
button1:addEventListener("touch", onMenuTouch)

-----------------------------------------------------------------------------------------

local textObjColor = display.newText("Tap to select", 150, 20, native.systemFont, 20)
textObjColor:setFillColor(0, 0, 0)
textObjColor.x = display.contentCenterX - 160
textObjColor.y = display.contentCenterY - 40

-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------


local validBallColors = {"yellow", "pink", "green", "aqua"}
                local currentBallColor = 0

                local function ballTapHandler( event )
                    currentBallColor = currentBallColor + 1

                    if currentBallColor > 4 then
                        currentBallColor = 1
                    end

                    if validBallColors[currentBallColor] == "yellow" then
                         event.target:setFillColor(255, 255, 0)
                         composer.setVariable("ballColor","yellow")

                    elseif validBallColors[currentBallColor] == "pink" then
                         event.target:setFillColor(1, 0.75, 0.75)
                         composer.setVariable("ballColor","pink")

                    elseif validBallColors[currentBallColor] == "green" then
                         event.target:setFillColor(0, 255, 0)
                         composer.setVariable("ballColor","green")

                    elseif validBallColors[currentBallColor] == "aqua" then
                         event.target:setFillColor(0, 255, 255)
                         composer.setVariable("ballColor","aqua")
                    end
                    return true
                end

		local ball = display.newImageRect( "ball.png", 25, 25 )  -- Changed to ball.png --
		ball.x = display.contentCenterX - 110   
		ball.y = display.contentCenterY - 10
        ball:addEventListener("tap", ballTapHandler)


-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------

	sceneGroup:insert( background )
	sceneGroup:insert( textObjColor )  
	sceneGroup:insert( button1 ) 
	sceneGroup:insert( ball )
end

-----------------------------------------------------------------------------------------

function scene:show( event )
	local sceneGroup = self.view
	local phase = event.phase
	
	if phase == "will" then
		-- Called when the scene is still off screen and is about to move on screen
	elseif phase == "did" then
		-- Called when the scene is now on screen
		-- 
		-- INSERT code here to make the scene come alive
		-- e.g. start timers, begin animation, play audio, etc.
		physics.start()
	end
end

-----------------------------------------------------------------------------------------

function scene:hide( event )
	local sceneGroup = self.view
	
	local phase = event.phase
	
	if event.phase == "will" then
		-- Called when the scene is on screen and is about to move off screen
		--
		-- INSERT code here to pause the scene
		-- e.g. stop timers, stop animation, unload sounds, etc.)
		physics.stop()
	elseif phase == "did" then
		-- Called when the scene is now off screen
	end	
	
end

-----------------------------------------------------------------------------------------

function scene:destroy( event )

	-- Called prior to the removal of scene's "view" (sceneGroup)
	-- 
	-- INSERT code here to cleanup the scene
	-- e.g. remove display objects, remove touch listeners, save state, etc.
	local sceneGroup = self.view
	
	package.loaded[physics] = nil
	physics = nil
end

-----------------------------------------------------------------------------------------

-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-----------------------------------------------------------------------------------------

return scene 

level1

local composer = require( "composer" )
local scene = composer.newScene()

-- include Corona's "widget" library 
local widget = require "widget"

-- include Corona's "physics" library
local physics = require "physics"
physics.start()
physics.setGravity( 0, 9.8 )

-----------------------------------------------------------------------------------------

-- forward declarations and other locals
local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX

function scene:create( event )

	local sceneGroup = self.view

	-- We need physics started to add bodies, but we don't want the simulaton
	-- running until the scene is on the screen.
	physics.start()
	physics.pause()

-----------------------------------------------------------------------------------------

	local function onMenuTouch(event)
		
		if(event.phase == "ended") then
			composer.gotoScene("menu", "fade")
		end

	end

-----------------------------------------------------------------------------------------

	-- display a background image --- 
	local background = display.newImageRect( "background.png", display.actualContentWidth, display.actualContentHeight )
	background.anchorX = 0
	background.anchorY = 0
	background.x = 0 + display.screenOriginX 
	background.y = 0 + display.screenOriginY

-----------------------------------------------------------------------------------------

--Function to handle button events ---
local function handleButtonEvent( event )
 
    if ( "ended" == event.phase ) then
        print( "Menu Button was pressed and released" )
    end
end
 
local button1 = widget.newButton(
    {
      label="Menu",
	  labelColor = { default={128}, over={128} },
        width = 55,
        height = 20,
        defaultFile = "menuBtnDefault.png",
        overFile = "menuBtnOver.png",
        onEvent = handleButtonEvent
                                           
    }
)
 
-- Center the button ---
button1.x = display.contentCenterX -250
button1.y = display.contentCenterY -140
button1:addEventListener("touch", onMenuTouch)

-----------------------------------------------------------------------------------------

								
local button2 = widget.newButton(
    {
      label="Replay", -- Replay Button doesn't work, don't know that code either --
	  labelColor = { default={128}, over={128} },
        width = 60,
        height = 20,
        defaultFile = "replayBtnDefault.png",
        overFile = "replayBtnOver.png",
        onEvent = handleButtonEvent
                                
    }
)
 
-- Center the button ---
button2.x = display.contentCenterX +250
button2.y = display.contentCenterY -140

-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------


		local ballColor = composer.getVariable("ballColor")

			local ball = display.newImageRect( "ball.png", 25, 25 ) -- Changed to ball.png --
			ball.x = 100   
			ball.y = 55

				if ballColor == "yellow" then
				    	ball:setFillColor(255, 255, 0)

				elseif ballColor == "pink" then
				     		ball:setFillColor(1, 0.75, 0.75)     	

				elseif ballColor == "green" then
				     		ball:setFillColor(0, 255, 0) 

				elseif ballColor == "aqua" then
				     		ball:setFillColor(0, 255, 255) 

				end

		physics.addBody( ball, "dynamic", { density=1.0, friction=0.5, bounce=0.1, radius=12 } ) 


-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------

local blackBlock = display.newImageRect( "blackBlock.png", 100, 40 )
blackBlock.x = 330
blackBlock.y = 190
physics.addBody( blackBlock, "static", { bounce=0.2 } ) 

-----------------------------------------------------------------------------------------
	
local greenBar1 = display.newImageRect("greenBar1.png", 70, 10) 
greenBar1.x = 98 
greenBar1.y = 88
greenBar1.rotation=0 
physics.addBody( greenBar1, "kinematic", { density=1.0, friction=0.3, bounce=1 } ) 

	local greenBar2 = display.newImageRect("greenBar2.png", 70, 10) 
	greenBar2.x = 200 
	greenBar2.y = 140
	greenBar2.rotation=0 
	physics.addBody( greenBar2, "kinematic", { density=1.0, friction=0.3, bounce=1 } ) 

-----------------------------------------------------------------------------------------
	
	sceneGroup:insert( background )
	sceneGroup:insert( button1 ) 
	sceneGroup:insert( button2 ) 
				sceneGroup:insert( ball ) -- ball --
	sceneGroup:insert( blackBlock ) 
	sceneGroup:insert( greenBar1 ) 
	sceneGroup:insert( greenBar2 ) 

------------------------------------------------------------------------------------------

local function doRectTouch( event )

        local touchedObject = event.target

            if event.phase == "began" then
                display.getCurrentStage():setFocus( touchedObject )  -- sets focus on greenBar --
                
                touchedObject.previousX = touchedObject.x
                touchedObject.previousY = touchedObject.y
                print("greenBar1 touched")


            elseif event.phase == "moved" then 
              
                touchedObject.y = ( event.y - event.yStart )  + touchedObject.previousY
                print("greenBar1 moved")


            elseif event.phase == "ended" or event.phase == "cancelled" then

                display.getCurrentStage():setFocus(nil) -- removes focus from greenBar --
                print("greenBar1 released")

    end
    return true
end 

greenBar1:addEventListener( "touch", doRectTouch ) 

-----------------------------------------------------------------------------------------

	local function greenBar1Tapped( event )
		      transition.to( greenBar1, { rotation=20, delta= true } ) 
		end

			greenBar1:addEventListener( "tap", greenBar1Tapped )
		
end

-----------------------------------------------------------------------------------------

function scene:show( event )
	local sceneGroup = self.view
	local phase = event.phase
	
	if phase == "will" then
		-- Called when the scene is still off screen and is about to move on screen
	elseif phase == "did" then
		-- Called when the scene is now on screen
		-- 
		-- INSERT code here to make the scene come alive
		-- e.g. start timers, begin animation, play audio, etc.
		physics.start()
	end
end

-----------------------------------------------------------------------------------------

function scene:hide( event )
	local sceneGroup = self.view
	
	local phase = event.phase
	
	if event.phase == "will" then
		-- Called when the scene is on screen and is about to move off screen
		--
		-- INSERT code here to pause the scene
		-- e.g. stop timers, stop animation, unload sounds, etc.)
		physics.stop()
	elseif phase == "did" then
		-- Called when the scene is now off screen
	end	
	
end

-----------------------------------------------------------------------------------------

function scene:destroy( event )

	-- Called prior to the removal of scene's "view" (sceneGroup)
	-- 
	-- INSERT code here to cleanup the scene
	-- e.g. remove display objects, remove touch listeners, save state, etc.
	local sceneGroup = self.view
	
	package.loaded[physics] = nil
	physics = nil
end

-----------------------------------------------------------------------------------------

-- Listener setup --
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-----------------------------------------------------------------------------------------

return scene

I sure fixing the colors is probably something easy that "I" just don't see.

 

 

I don't mean to be a pain, but is there no simple way to use png's instead of fill colors? I made some nice png balls with Inkscape.

 

 

Thanks again



[TOPIC: post.html]
#9

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,085 posts
  • Corona SDK

An easier way of doing this is to pass the colour as a parameter to the next scene.  So you could do this

composer.gotoScene("level", {effect = "fade", time = 400, params = {ballColour = "red"}})

Then you can get that colour in the next scene like this

local ballColour = event.params.ballColour

You can then simply load the correct png

local ball = display.newImageRect( ballColour..".png", 25, 25 )


[TOPIC: post.html]
#10

Jack G

[GLOBAL: userInfoPane.html]
Jack G
  • Observer

  • 10 posts
  • Corona SDK

Thank you Rob and SGS for helping me out.



[TOPIC: post.html]
#11

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,684 posts
  • Enterprise

For future reference, dumping full scenes, or in this case your whole project isn't very helpful. First we can't build your project because we don't have any of your art or audio assets. Secondly that much text, most people are going to simply skim passed. You're lucky you got a couple of people to take the time to read through all the code.

 

It's always best if you can narrow down the problem, but in this case, seeing how your app is organized was important to offer a suggestion. In the future put your entire project in a .zip file, upload it to DropBox, Google Drive or similar and provide a link so we can download the file and see the whole project and not make forum posts that take forever to read.

 

Thanks for understanding!

 

Rob




[topic_controls]
[/topic_controls]