Generating alphabetical shapes by adding an object

Hello,

I am making a game in which I am taking user input of any alphabet and generating that aphabetical shape by positioning an object in that shape. For now, I have a function A() and in that function I have placed an object(balloon image) dynamically in such a position that it form alphabet A. I am calling this function at runtime by user input. Now I want all 26 alphabets in this manner. But I dont want to hard code for this. Please anyone suggest any simpler way to do this.

Thanks in advance.

if the issue is avoiding a ton of “if letter == “a” then” and so on, you can use a table that has letters as index, holding the various functions you’ve built.
For example;

 

local createLetterShape; local A, B, C...; function A() ... end function B() ... end function C() ... createLetterShape = {}; createLetterShape["a"] = A; createLetterShape["b"] = B; createLetterShape["c"] = C; ... --Now when we need to draw a specific letter we can do: local letter = "A"; --holds the letter the user has typed createLetterShape[string.lower(letter)](); --with string.lower it doesn't matter if the user types a lower or upper case letter

Thanks, but what I want is to know how I can create those alphabetical shapes without any hard coding or placing dynamically objects.

Please tell me how can I achieve this.

There wasn’t much to do apart from hardcoding them before, but with the new Corona API to outline the shape of a texture, it should be easy enough (; Take a look here: http://coronalabs.com/blog/2014/01/28/tutorial-create-physics-bodies-from-texture-assets/

Coincidentally, they’re actually showing physics bodies given to letters from an imageSheet (:

Thanks. Actually what I want is to get letter shaped texture by placing multiple objects in that respect.

Right now I am doing is

function Z(view) print("LETTER Z") local x = 500 local y = 300 local myImage = display.newImage(view, "balloon.png" ) myImage.x = x myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 40 myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 80 myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 120 myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 160 myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 200 myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 240 myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 280 myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+260 myImage.y = y+50 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+210 myImage.y = y+90 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+170 myImage.y = y+130 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+130 myImage.y = y+170 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+90 myImage.y = y+210 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+50 myImage.y = y+240 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x myImage.y = y+290 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+40 myImage.y = y+290 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+80 myImage.y = y+290 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+120 myImage.y = y+290 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+160 myImage.y = y+290 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+200 myImage.y = y+290 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 240 myImage.y = y+290 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 280 myImage.y = y+290 myImage.myName="letter"; end

In this Z function I am placing multiple object by placing balloon image multiple times in such a way that all these will form letter Z shape.

But for this I had to hardcode for placing each object. And for all 26 letters I had to do same hardcoding.

Please tell me if there is any solution for this problem.

Thanks in advance.

Unfortunately there’s not a super immediate solution.
What I would do to simplify the process would be to first grab the x and y position by using a touch sensor as big as the screen. You click on the sensor and print out the x and y position (with an offset such as the center of the screen, so you can position them wherever you want by changing that offset later) of the touch. Next, you put them all in a table, and loop through them to create the objects, like this for example:

local createBalloonObjects = function(data, xPos, yPos, letter) for i = 1, #data do local balloon = display.newImage(view, "balloon.png"); balloon.x, balloon.y = xPos+data[i].x, yPos+data[i].y; balloon.myName = letter; end end local letterAdata = { {x = 50, y = 50}, {x = 70, y = 30}, ... }; createBalloonObjects(letterAdata, 100, 100, "A");

if the issue is avoiding a ton of “if letter == “a” then” and so on, you can use a table that has letters as index, holding the various functions you’ve built.
For example;

 

local createLetterShape; local A, B, C...; function A() ... end function B() ... end function C() ... createLetterShape = {}; createLetterShape["a"] = A; createLetterShape["b"] = B; createLetterShape["c"] = C; ... --Now when we need to draw a specific letter we can do: local letter = "A"; --holds the letter the user has typed createLetterShape[string.lower(letter)](); --with string.lower it doesn't matter if the user types a lower or upper case letter

Thanks, but what I want is to know how I can create those alphabetical shapes without any hard coding or placing dynamically objects.

Please tell me how can I achieve this.

There wasn’t much to do apart from hardcoding them before, but with the new Corona API to outline the shape of a texture, it should be easy enough (; Take a look here: http://coronalabs.com/blog/2014/01/28/tutorial-create-physics-bodies-from-texture-assets/

Coincidentally, they’re actually showing physics bodies given to letters from an imageSheet (:

Thanks. Actually what I want is to get letter shaped texture by placing multiple objects in that respect.

Right now I am doing is

function Z(view) print("LETTER Z") local x = 500 local y = 300 local myImage = display.newImage(view, "balloon.png" ) myImage.x = x myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 40 myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 80 myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 120 myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 160 myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 200 myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 240 myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 280 myImage.y = y myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+260 myImage.y = y+50 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+210 myImage.y = y+90 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+170 myImage.y = y+130 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+130 myImage.y = y+170 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+90 myImage.y = y+210 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+50 myImage.y = y+240 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x myImage.y = y+290 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+40 myImage.y = y+290 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+80 myImage.y = y+290 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+120 myImage.y = y+290 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+160 myImage.y = y+290 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x+200 myImage.y = y+290 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 240 myImage.y = y+290 myImage.myName="letter"; local myImage = display.newImage(view, "balloon.png" ) myImage.x = x + 280 myImage.y = y+290 myImage.myName="letter"; end

In this Z function I am placing multiple object by placing balloon image multiple times in such a way that all these will form letter Z shape.

But for this I had to hardcode for placing each object. And for all 26 letters I had to do same hardcoding.

Please tell me if there is any solution for this problem.

Thanks in advance.

Unfortunately there’s not a super immediate solution.
What I would do to simplify the process would be to first grab the x and y position by using a touch sensor as big as the screen. You click on the sensor and print out the x and y position (with an offset such as the center of the screen, so you can position them wherever you want by changing that offset later) of the touch. Next, you put them all in a table, and loop through them to create the objects, like this for example:

local createBalloonObjects = function(data, xPos, yPos, letter) for i = 1, #data do local balloon = display.newImage(view, "balloon.png"); balloon.x, balloon.y = xPos+data[i].x, yPos+data[i].y; balloon.myName = letter; end end local letterAdata = { {x = 50, y = 50}, {x = 70, y = 30}, ... }; createBalloonObjects(letterAdata, 100, 100, "A");