Physics editor and sprite sheets - corona - coronasdk

I'm trying to create a collision for my sprite sheet objects using Physics Editor, however I can only edit 1 image of the 2 that I have in my sprite sheet. How can I edit them both?
I made my sprite sheet through Photoshop.
Cheers

You have to separate the sprite in PhotoShop first then go to Physics Editor and make one physics body for each sprite then you can put them together in Corona SDK.
I did like that for my game and it works.
https://play.google.com/store/apps/details?id=com.bgames.h_balls
--// Wall RIGHT
local scaleFactor = 1.0;
local physicsData = (require "wall_right").physicsData(scaleFactor);
local wall_R = display.newImageRect(WALL_Group, "img/wall.png", 448, 397);
physics.addBody( wall_R, physicsData:get("wall") )
wall_R.bodyType = "static";
--// Wall LEFT
local scaleFactor = 1.0;
local physicsData = (require "wall_left").physicsData(scaleFactor);
local wall_L = display.newImageRect(WALL_Group, "img/wall.png", 448, 397);
physics.addBody( wall_L, physicsData:get("wall") )
wall_L.bodyType = "static";
This is the code i used for my game.

Related

cocos2dx3.3 camera doesn't move with sprite

I am trying to use cocos2dx 3.3 to build games.
we create a physicisworld with
cc.Scene:createWithPhysics()
and i add a camera like this
local camera = cc.Camera:createOrthographic(width,height,0,1)
camera:setCameraFlag(cc.CameraFlag.USER1)
camera:setPosition3D(cc.vec3(0, 0, 0))
and I add this camera to the layer
I just want to bind the sprite with camera
so I code in layer function update
function PlayLayer:update(delta)
--camera
local camera = self.camera
local speed = 100*delta
local winsize = cc.Director:getInstance():getWinSize()
local player = self.player
player:update()
player:setX(player:getX() + speed)
local postiony = 180
self.camera:setPositionX(player:getX() )
end
I set the camera move with sprite,however the sprite is moving and the position of camera is also changed. but, there is no effect. the sprite just move out of the screen.
Am I wrong. please help me with this question. thank you!

box 2d concepts :Gideros (LUA)

In below mentioned code, what exactly is the difference between sprite, body and ground? Why are there no parameters passes to createBody while declaring ground?
what is difference between edgeshape.new() and polygonshape.new()?
require "box2d"
b2.setScale(20)
-- this function creates a box sprite with 2 happy and sad children
local function createBoxSprite(sx, sy)
local happy = Bitmap.new(Texture.new("happy-box.png", true))
happy:setAnchorPoint(0.5, 0.5)
local sad = Bitmap.new(Texture.new("sad-box.png", true))
sad:setAnchorPoint(0.5, 0.5)
local sprite = Sprite.new()
sprite:addChild(happy)
sprite:addChild(sad)
sprite:setScale(sx, sy)
return sprite
end
-- this table holds the dynamic bodies and their sprites
local actors = {}
-- create world
local world = b2.World.new(0, 9.8)
-- create a ground body and attach an edge shape
local ground = world:createBody({})
local shape = b2.EdgeShape.new(-200,480,520,480)
ground:createFixture({shape = shape, density = 0})
-- every 3 seconds, we create a random box
local function onTimer()
local sx = math.random(70, 100) / 100
local sy = math.random(70, 100) / 100
local body = world:createBody{type = b2.DYNAMIC_BODY, position = {x = math.random(0, 320), y = -35}}
local shape = b2.PolygonShape.new()
-- box images are 70x70 pixels. we create bodies 1 pixel smaller than that.
shape:setAsBox(34.5 * sx, 34.5 * sy)
body:createFixture{shape = shape, density = 1, restitution = 0.1, friction = 0.3}
local sprite = createBoxSprite(sx, sy)
stage:addChild(sprite)
actors[body] = sprite
end
Sprite is an empty object to group other Sprite inherited objects as in your case happy and sad Bitmaps.
Body is a representation of the physical body in box2d, it does not have any visual representation, only numbers as dimensions (width and height), position, rotation and different forces. Usually inside ENTER_FRAME event you take this values as position and rotation and apply them to the Sprite inherited object, so it would move exactly like the body in box2d simulated world.
Ground is an empty dummy box2d object. If you don't provide parameters to createBody it means the defaults are used, which basically means you don't care what values it has. The most common example of such body usage is for joints.
To create a joint you usually need two bodies, but what if you would to attach body through joint to some random position in the air, or dynamic position as mouse cursor, well you can do it by creating dummy physics object
Here is an example: http://appcodingeasy.com/Gideros-Mobile/Dragging-Box2d-object-in-Gideros-Mobile
In case of your example, then this body is used for EdgeShape which is usually an arbitrary shape used for creating world boundaries or in this case simply ground, so the dynamic object won't fall outside the screen

Image displays in top left corner in Corona Simulator

I just started playing with lua / Corona, and have been using the book "Create Mobile Games with Corona." (Silvia Domenech) The first project in the book is to make a simple planet defense game. For this, I am using a multi-screen application (or "scene" as it's called in OS X). The very first step is displaying an image using groups in Corona.
The book has me enter the following code in the main scene group:
local image = display.newImage( "images/iphone_767.png")
group:insert( image )
After adding it to (what I think is) the correct group, the code looks like this:
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
-----------------------------------------------------------------------------
local image = display.newImage( "images/iphone_767.png")
group:insert( image )
-- CREATE display objects and add them to 'group' here.
-- Example use-case: Restore 'group' from previously saved state.
-----------------------------------------------------------------------------
end
For the image, I first tried an image that was 320 x 480. When it rendered on the simulator, it is situated way off to the top left of the simulator. Here is a screenshot of how it renders: http://imgur.com/Kpm0XTd
The config file is set for 320 x 480 px. I'm really at a loss as to what could be causing this since I haven't modified anything outside of what I described. Any ideas?
Set the x and y values of the token to screen center (assuming you want to center it):
image.x = display.contentWidth/2
image.y = display.contentHeight/2
Thanks
Anand
I personally like setting a variable to display.contentWidth and display.contentWidth.
_W = display.contentWidth
_H = display.contentHeight
image.x = _W/2
image.y = _H/2
You could also format it like so:
image.x = _W*.5
image.y = _W*.5
Corona SDK has recently started using a new graphics engine that affects how things are positioned. The display.newImage() used to draw the top left corner at 0, 0, but now it draws the center at 0, 0. Your best bet is to always explicitly set the .x and .y of the image where you want it.
Seems there is simplified way exists now:
image.x = display.contentCenterX
image.y = display.contentCenterY
Or single line:
local image = display.newImage('images/iphone_767.png', display.contentCenterX, display.contentCenterY)
But now it is seems more conventional pattern is to create centeredGroup first:
-- Create centered group
local centeredGroup = display.newGroup()
centeredGroup.x = display.contentCenterX
centeredGroup.y = display.contentCenterY
-- Add background
local image = display.newImage(centeredGroup, 'images/iphone_767.png', display.contentCenterX, display.contentCenterY)

Corona "Attempt to remove an object that has already been removed"

I'm working on a simple "breakout" game and I have problem reloading a map.
for example: if I start with level1, break some bricks and lose, than I'm loading the same map again. next time that the ball collides with the same brick I "touched" before, will give me an error Attempt to remove an object that has already been removed
local map = lime.loadMap("maps/" .. currentLevel .. ".tmx")
local layer = map:getTileLayer("bricks_1")
local visual = lime.createVisual(map)
local physical = lime.buildPhysical(map)
function removeBricks(event)
if event.other.isBrick then
local brick = event.other
transition.to(brick, {time = 20, alpha = 0})
score = score + brick.scoreValue
ScoreNum.text = score
-- remove brick
brick:removeSelf()
brick = nil
...
i think the second time you go to your game the event.other is not created are you using storyboard if so you can try to remove the scene after the game is over so when you go to your game again it will recreate all the object
Have you try this?
transition.to(brick, {time = 20, alpha = 0, onComplete = function()
if brick then
brick:removeSelf()
brick = nil
end
end})
If you are using physics you also have to do a physics.removeBody(brick) before you remove the object itself so that it detaches from the physics engine. If not physics thinks it's still there.

Getting character to jump

I've created a sprite sheet(of a frog jumping) using texture packer and I am trying to get the character to jump forward when I click on the sprite. I've created an event listener and when I click on the sprite the play() method animates the sprite. But I can't get the sprite to jump forward using the applyForce or setLinearVelocity methods? Here is my code:
require("physics")
local sprite = require "sprite"
local sheetData = require "myFrogs" -- name of file created using texturepacker
physics.start()
physics.setGravity(0,1)
local _w = display.contentWidth/2
local _h = display.contentHeight/2
local spriteData = sheetData.getSpriteSheetData()
local spriteSheet = sprite.newSpriteSheetFromData("images/myFrogs.png", spriteData)
local spriteSet = sprite.newSpriteSet(spriteSheet, 1, 7) number of images in spritesheet
local frogSprite = sprite.newSprite(spriteSet)
frogSprite.x = _w
frogSprite.y = _h
physics.addBody(frogSprite, "static", {friction=1.0,density=1.0,bounce=0.3, radius=35})
frogSprite.isFixedRotation = true
local function frogJump(event)
if(event.phase == "ended") then
--frogSprite:applyForce() -- should I use this method
--frogSprite:setLinearVelocity() -- or this method
frogSprite:play()
end
end
frogSprite:addEventListener("touch", frogJump)
You made the frog's body static - switch to dynamic and it will jump using either of those methods.
By making frog body static, it will not be affected by impulse or force so it should be Dynamics
Body:applyForce( xForce, yForce, bodyX, bodyY ) <- Apply Force to your center of mass i.e Reference point you have set.
Body:setLinearVelocity( xVelocity, yVelocity ) <- It will give a velocity of data to will provide in terms of pixel per second.

Resources