Archive for 'Png'

Getting Bounds of an Display Object in corona sdk

you can get the display object bounds using object.contentBounds

Example:

local img = display.newImage('square.png');

local bounds = img.contentBounds 
print("xMin: ".. bounds.xMin) 
print("yMin: ".. bounds.yMin)
print("xMax: ".. bounds.xMax) 
print("yMax: ".. bounds.yMax) 

For performing a dissolve transition, we need two images

--first image
local img1 = display.newImageRect("square.png", 112, 112);
img1.x = display.contentWidth * 0.5;
img1.y = display.contentHeight * 0.4;
img1.alpha = 1;

--second image

local img2 = display.newImageRect("circle.png", 112, 112);
img2.x = display.contentWidth * 0.5;
img2.y = display.contentHeight * 0.4;
-- we must hide the second image before animation
img2.isVisible = false;

--performs transition
transition.dissolve(img1, img2, 1000, 0);

A simple code snippet in corona for drag and drop

A simple code snippet in corona  for drag and drop:

local function onTouch( event )

local t = event.target
local phase = event.phase

if “began” == phase then

t.isFocus = true
t.x0 = event.x – t.x
t.y0 = event.y – t.y

elseif t.isFocus then

if “moved” == phase then

t.x = event.x – t.x0
t.y = event.y – t.y0

elseif “ended” == phase or “cancelled” == phase then

display.getCurrentStage():setFocus( nil )
t.isFocus = false
t.setFillColor( 0, 255, 0 )

end
end

return true

end

local function onComplete( event )
print “Completed”
end

local rect = display.newRect( 50, 50, 100, 100)
rect:setFillColor( 255, 0, 0 )
rect:addEventListener( “touch”, onTouch )

local circle = display.newCircle(100, 70, 30)
circle:setFillColor( 0, 255, 0 )
circle:addEventListener( “touch”, onTouch )

local abc = display.newImage(“button.png”)
abc.x = display.contentWidth / 2
abc.y = display.contentHeight / 2

abc:addEventListener( “tap”, abc )

function abc:tap( event )
print “button clicked”
end

Convert UIImage to NSData

UIImage *img = [UIImage imageNamed:@"some.png"];

NSData *dataObj = UIImageJPEGRepresentation(img, 1.0);

Creating Body in Corona

local yourBody = display.newImage( "yourImage.png" )
yourBody.x = 50; yourBody.y = 250; yourBody.rotation = -12
physics.addBody( yourBody, "static", { friction=0.5 } )

It is useful,if we make an objects into a group. so that we can easily add and remove the objects from a group and it will not affect other objects.The following code will add the objects into a group.

local my_group = display.newGroup();

local img1 = display.newImage( "sky.png", true )
my_group:insert( sky )
img1.x = 160; sky.y = 160

local img2 = display.newImage( "sky.png", true )
my_group:insert( img2 )
img2 =.x = 1120; img2.y = 160

Adding body to the image in corona sdk

We can add body to an image using the following code

local board = display.newImage( "board.png" )
physics.addBody( board, { density=0.8, friction=0.3, bounce=0.3 } )

enabling retina display mode in corona sdk

for enabling retina display in corona add the following lines in config.lua

application = 
{
	content = 
	{ 
		width = 320,
		height = 480, 
		scale = "letterbox",
		imageSuffix =
			{
				["-x15"] = 1.5,		-- A good scale for Droid, Nexus One, etc.
				["-x2"] = 2,		-- A good scale for iPhone 4 and iPad
			},
	}
}

and include images with twice and 1.5 larger than the original and named as image-x2.png and image-x15.png in the project folder

Enabling Retina Display mode

You should enable RetinaDisplay mode ONLY if you want to use HighRes images on an iPhone4. Remember that an iPhone4 also works in “low res” mode.

// Add this code in your Application Delegate, right after initializing the director

// Director Initialization
[director setOpenGLView:glView];
 
// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [director enableRetinaDisplay:YES] )
	CCLOG(@"Retina Display Not supported");

then keep both normal and hd images (with extension image-hd.png).

NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

path = [[pathArr objectAtIndex:0]stringByAppendingPathComponent:[NSString stringWithFormat:@"imagename.png"]];  // Getting the path of the image, whose size is to be calculated.

NSFileManager *fileManager = [NSFileManager defaultManager]; // Creating the object of file manager.

NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:path traverseLink:YES]; // Getting the attributes of the file in the path.

if(fileAttributes != nil)

{

NSString *fileSize = [fileAttributes objectForKey:@"NSFileSize"]; // Getting the value corresponding to the key NSFileSize from the attributes array and storing it to a string..

NSLog(@"File size: %@ kb", fileSize);

}
Page 2 of 41234
We Can Build Your App for You. Lets Get Started !