I am working on a game using Corona SDK , I have number of balls to display in game . i have implemented TouchListener to all of ball objects .
Code is here
local function ballTouchEvent(e)
local touchedBall = e.target
local phase = e.phase
if phase == "began" then
log("Touch began Phase")
elseif phase == "moved" then
log("Moved Phase")
elseif phase == "ended" or phase == "cancelled" then
log("Ended Phase")
end
return true
end
ball:addEventListener("touch",ballTouchEvent)
I want to implement some functionality when user touches on any of shown ball and moves his touch to white background (Place having no Ball) . Can any one guide me how to implement this ? Thanks in advance
Implement a React behind all of balls and implement click listener to that react. SO that when user leave the touch on white space, the ended phase of react listener will be called and you can put your implementation there what you want to do .
function scene:create( event )
sceneGroup = self.view
local rect = display.newRect(centerX, centerY, constants.screenWidth, constants.screenHeight)
-- rect:setFillColor( 0.0 )
rect.name = "background"
rect:addEventListener("touch",backTouchEvent)
sceneGroup:insert( rect )
end
You can add a group and then handle touch listener and do your work in group's end touch call.
Please visit the following link
Touch Event detection issue
Related
So I'm making a Pong game in unity and I wanted to port it to Android but the problem is I don't realy know how to make it work on mobile. The idea was to just tap on the side of the screen for the paddle to move but Unity Input Manager Doesn't really support things like that and I heard about custom Input Managers but all the tutorials just can't help so I need to ask you. https://i.stack.imgur.com/uB9q0.png
You need to check for touches, get the touch and update the transform of whatever object you're touching(hopefully).
Documentation of concern:
Touch
Touch.phase
Transform
Camera.ScreenToWorldPoint
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0); // get first touch since touch count is greater than zero
if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
{
// get the touch position from the screen touch to world point
Vector3 touchedPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
// lerp and set the position of the current object to that of the touch, but smoothly over time.
transform.position = Vector3.Lerp(transform.position, touchedPos, Time.deltaTime);
}
}
I think this should be very easy to do, but I'm not able to find any solution. It's always just system.enable("multitouch") and then something with setFocus() method.
I have a right button, that responds to touch event and makes player run right when held. Then I have a jump button, that responds to tap event, making player jump.
Running works, jumping works, but when I run and try to jump nothing happens.
Here is some code:
local speed = 3
local motionx = 0
-- When right arrow is touched, move character right
function right:touch()
if string.find(player.sequence,"jump") then
player:setSequence("jumpRight")
motionx = speed;
elseif string.find(player.sequence,"duck") then
player:setSequence("duckRight")
else
player:setSequence("goingRight")
motionx = speed;
end
player:play()
end
right:addEventListener("touch",right)
-- When up arrow is tapped, jump character
function jump:tap()
if not isJumping then
if string.find(player.sequence, "goingRight") then
isJumping = true
player:setSequence("jumpRight")
player:setLinearVelocity(0,-120)
end
if string.find(player.sequence, "goingLeft") then
isJumping = true
player:setSequence("jumpLeft")
player:setLinearVelocity(0,-120)
end
if string.find(player.sequence, "duckLeft") then
player:setSequence("goingLeft")
end
if string.find(player.sequence, "duckRight") then
player:setSequence("goingRight")
end
player:play()
end
end
jump:addEventListener("tap",jump)
-- Move character
local function movePlayer (event)
player.x = player.x + motionx;
end
Runtime:addEventListener("enterFrame", movePlayer)
I don't knowh where, how or why should I use the setFocus() method. But so far when running, I cannot jump until I release the right button.
It was my mistake, that I wanted to use TAP event for jump. I guess since it is called multiTOUCH it only works with multiple TOUCH events :) I changed TAP events to TOUCH events and added:
if event.phase == "began" then
display.getCurrentStage():setFocus( event.target, event.id )
--other code
end
if event.phase == "ended" or event.phase == "cancelled" then
display.getCurrentStage():setFocus( event.target, nil )
end
Now it works nicely.
I develop a game that need user to have a single touch to draw over to link multiple objects. Currently using mouse button as touch input with this code:
if(Input.GetButton("Fire1"))
{
mouseIsDown = true;
...//other codes
}
It works fine with mouse but it will give me problem when I compile it for multi touch device. In multitouch device, if you have 2 fingers down, the middle point will be taken as the input. But if I already touch an object then with second finger down to screen, it will mess up my game input and give me havoc design.
What I want is to limit it to 1 touch only. If the second finger come touching, ignore it. How can I achieve that?
Below is all you need:
if ((Input.touchCount == 1) && (Input.GetTouch (0).phase == TouchPhase.Began)) {
mouseIsDown = true;
...//other codes
}
It will only fire when one finger is on the screen because of Input.touchCount == 1
You are using Input.GetButton. A button is not a touch. You probably want to look at Input.GetTouch
If you need to support multiple input-type devices, you may want to consider scripting your own manager to abstract this out somewhat.
I would roll with a bit of polling!
so in your Update() I typically do something like this:
foreach (Touch touch in Input.touches)
{
// Get the touch id of the current touch if you're doing multi-touch.
int pointerID = touch.fingerId;
if (touch.phase == TouchPhase.Began)
{
// Do something off of a touch.
}
}
If you're looking for more info check this out:
TouchPhases in Unity
I'm trying to get some collision detection working but I can't seem to figure out what the problem is.
The error I'm getting is
...\main.lua:178:attempt to concatenate field 'name' (a nil value)
What I have is this: I have a box (the "ship") that stays at a fixed X coordinate, but moves up and down. It's meant to go through a "tunnel" made up of two rectangles with a gap between them, and I'm trying to detect a collision between the ship and the walls of the tunnel (ie the rectangles).
I get this error when the collision happens. A lot of my code is just modified versions of the official Corona documentation and I'm just not able to figure out what the problem is.
Here are the relevant pieces of code:
function playGame()
-- Display the ship
ship = display.newRect(ship);
shipLayer:insert(ship);
-- Add physics to ship
physics.addBody(ship, {density = 3, bounce = 0.3});
...
beginRun();
end
function beginRun()
...
spawnTunnel(1100); -- this just calls the createTunnel function at a specific location
gameListeners("add");
...
end
function gameListeners(event)
if event == "add" then
ship.collision = onCollision;
ship:addEventListener("collision", ship);
-- repeat above two lines for top
-- and again for bottom
end
end
-- Collision handler
function onCollision(self,event)
if ( event.phase == "began" ) then
-- line 178 is right below this line ----------------------------------
print( self.name .. ": collision began with " .. event.other.name )
end
-- Create a "tunnel" using 2 rectangles
function createTunnel(center, xLoc)
-- Create top and bottom rectangles, both named "box"
top = display.newRect(stuff);
top.name = "wall";
bottom = display.newRect(stuff);
bottom.name = "wall";
-- Add them to the middleLayer group
middleLayer:insert(top);
middleLayer:insert(bottom);
-- Add physics to the rectangles
physics.addBody(top, "static", {bounce = 0.3});
physics.addBody(bottom, "static", {bounce = 0.3});
end
I only get the error message once the two objects should collide, so it seems like the collision is happening, and it is being detected. But for some reason self.name and event.other.name are nil.
Try to use :
top.name = "wall";
and
bottom.name = "wall";
as
top.myName = "wall"
and
bottom.myName = "wall";
Use onCollision function after your "createTunnel: function :
-- Collision handler
function onCollision( self, event)
if ( event.phase == "began" ) then
print( self.myName .. ": collision began with " .. event.other.myName )
end
end
Oh wow. After many hours I finally figured out my stupid, simple mistake.
The problem was that I forgot to give the ship its own name. The code now looks like this and works just fine:
function playGame()
-- Display the ship
ship = display.newRect(ship);
ship.name = "ship"; -- added this line to give the ship a name
shipLayer:insert(ship);
-- Add physics to ship
physics.addBody(ship, {density = 3, bounce = 0.3});
...
beginRun();
end
I am writing in Lua, using the Corona SDK, and I am looking to make an object inactive after a collision.
function onCollision(event)
if event.phase == "began" then
bullet.collided = true
bullet.isVisible = false
bullet:applyLinearImpulse(-5, 0, bullet.x, bullet.y)
explode(event)
end
end
function explode(event)
local x = event.object2.x
local y = event.object2.y
explosion.x = x
explosion.y = y
explosion.isVisible = true
explosion:play()
resetExplosion()
end
The above function takes the single bullet on the screen and makes it invisible after a collision with a ball that it shoots in the y axis. It then applys an impulse to remove it from the screen in the x-axis. My issue is that the ball (object2) in the collision is invisible after collision as well, but it still can be hit by the new bullet. There is only one bullet, so I can directly say bullet.whatever, but there is an array of balls, so the ball has to be addressed like ball[i].whatever.
is there a way to pass the index, i through the onCollision function?
You can set the body to inactive if you use a slight delay in your collision handler.
i.e:
-- Inside your Collision event
local function delay()
--Change the body's active state to false
body.isBodyActive = false
end
timer.performWithDelay( 10, delay )
According to CoronaSDK chapter about event.collision event.object1 and event.object2 are the properties identifying those collided objects (references to the bullet and one of the balls in Your case). So, doesn't it solve Your problem?