У меня новая проблема .. Я не могу ее исправить ..
Я создаю как шар, и когда я щелкаю, шар идет туда, куда я щелкал.Так что все в порядке.Я заполняю кадрами некоторые самолеты, которые падают под действием силы тяжести.
Но у меня есть проблема, потому что мое столкновение не стреляет, когда шар и самолет касаются себя ...
Можеткто-нибудь Помогите мне ?Это мой код.
--------------------------------------------------------
-- CHARACTER PART --
--------------------------------------------------------
-- Creating the character --
local _H = display.contentHeight;
local _W = display.contentWidth;
local character = display.newImageRect("images/small-black.png",80,80);
character:setReferencePoint(display.CenterReferencePoint);
character.x = _W/2; character.y = _H/2;
character.color = "reddish";
-- This is necessary so we know who hit who when taking care of a collision event
character.name = "character"
-- Listen to collisions
character.collision = onCollision
character:addEventListener("collision", character)
-- Text to know the phase--
local txt = display.newText("click state",0, 0, native.systemFont, 16*2);
txt.xScale = 0.5; txt.yScale = 0.5;
txt:setReferencePoint(display.CenterReferencePoint);
txt.x = _W/2; txt.y = _H/2;
-- Function on touch --
function character:touch(e)
if(e.phase == "began") then
txt.text = "Began Phase";
display.getCurrentStage():setFocus(e.target, e.id);
elseif(e.phase =="moved") then
txt.text = "Moved phase";
elseif(e.phase == "ended") then
txt.text = "Ended phase";
transition.to(self, {x = e.x, y = e.y, time=800});
transition.to(self, {x = e.x, y = e.y, time=800});
end
end
-- Add event --
character:addEventListener("touch", character);
--------------------------------------------------------
-- ENNEMY PART --
--------------------------------------------------------
-- Load and start physics
local physics = require("physics")
physics.start()
-- Gravity --
physics.setGravity(0, 20)
-- Layers (Groups) --
local gameLayer = display.newGroup()
local enemiesLayer = display.newGroup()
-- Declare variables
local gameIsActive = true
local halfPlayerWidth
local textureCache = {}
textureCache[1] = display.newImage("images/enemy.png"); textureCache[1].isVisible = false;
local halfEnemyWidth = textureCache[1].contentWidth * .5
gameLayer:insert(enemiesLayer)
-- Take care of collisions
local function onCollision(self, event)
-- Player collision - GAME OVER
if self.name == "character" and event.other.name == "enemy" and gameIsActive then
local gameoverText = display.newText("Game Over!", 0, 0, "HelveticaNeue", 35)
gameoverText:setTextColor(255, 255, 255)
gameoverText.x = display.contentCenterX
gameoverText.y = display.contentCenterY
gameLayer:insert(gameoverText)
-- This will stop the gameLoop
gameIsActive = false
end
end
-- Add to main layer
gameLayer:insert(character)
-- Store half width, used on the game loop
halfPlayerWidth = character.contentWidth * .5
-- GAME LOOP --
local timeLastEnemy = 0, 0
local function gameLoop(event)
if gameIsActive then
-- Remove collided enemy planes
--for i = 1, #toRemove do
--toRemove[i].parent:remove(toRemove[i])
--toRemove[i] = nil
--end
-- Check if it's time to spawn another enemy,
-- based on a random range and last spawn (timeLastEnemy)
if event.time - timeLastEnemy >= math.random(600, 1000) then
-- Randomly position it on the top of the screen
local enemy = display.newImage("images/enemy.png")
enemy.x = math.random(halfEnemyWidth, display.contentWidth - halfEnemyWidth)
enemy.y = -enemy.contentHeight
-- This has to be dynamic, making it react to gravity, so it will
-- fall to the bottom of the screen.
physics.addBody(enemy, "dynamic", {bounce = 0})
enemy.name = "enemy"
enemiesLayer:insert(enemy)
timeLastEnemy = event.time
end
end
end
-- Call the gameLoop function EVERY frame,
-- e.g. gameLoop() will be called 30 times per second ir our case.
Runtime:addEventListener("enterFrame", gameLoop)