Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Arrange shapes in a line equally and stop duplication
Started by dip Aug 14 2018 03:47 AM

10 replies to this topic
lua corona tables spawning
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

dip

[GLOBAL: userInfoPane.html]
dip
  • Contributor

  • 142 posts
  • Corona SDK

I have a few colours stored inside a table

local xLocation = _W/2         -- _W/2 & _H/2 is basically centre of screen 
local yLocation = _H/2 - 100

local allColors = {
    {name="Red", image = "Shapes/redDiamond.png",height = 43, width = 43, x=xLocation +10, y=yLocation },
    {name="Blue", image = "Shapes/blueDiamond.png", height = 43, width = 43, x=xLocation- 10, y=yLocation },
    {name="Green", image = "Shapes/greenDiamond.png", height = 43, width = 43, x=xLocation+30, y=yLocation},
    {name="Yellow", image = "Shapes/yellowDiamond.png", height = 43, width = 43, x=xLocation-30, y=yLocation},
    {name="Purple", image = "Shapes/Purple.png", height = 43, width = 43, x=xLocation-60, y=yLocation}
}

And I have another table for storing chosen colours 

local choosenColors = {} -- Stores 4 random colours from allColors table

Finally the function that picks the 4 random colors, inserts them into choosenColours, and then displays them. 

while #choosenColors < 4 do
        n = math.random(1, #allColors) 
        table.insert(choosenColors, allColors[n])
        for i=1, #(choosenColors) do 
            local s = display.newImageRect(choosenColors[i].image, choosenColors[i].height, choosenColors[i].width)
            s.x = choosenColors[i].x
            s.y = choosenColors[i].y
            baseGroup:insert(s)
        end
    end
        --table.remove(allColors, n)

    for k, v in pairs(choosenColors) do
        print(k, v)
    end

The issue

I'd like to point out that I am aware I have hard-coded all the positions manually, this was just for testing purposes to see if the function was displaying everything inside the chosenColours function.

 

However, now that it is working, im stuck trying to figure out how I would go about putting the objects in a line equally spaced. For example...

 

O   O   O   O 

 

Also how to then stop duplicates inside the chosenColours table from happening. 

 

Anyone can help me out on this would be greatly appreciated. 

 



[TOPIC: post.html]
#2

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,347 posts
  • Enterprise

1.  calculate "s.x" as f.e. #chosenColors*50+50

2. shuffle allColors, then take the first 4 as chosenColors



[TOPIC: post.html]
#3

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 389 posts
  • Corona SDK

Hi dip,

 

Try 

-- Use your own values
local spacing, startX = 20, 100

s.x = startX + ( i - 1 ) * ( s.width + spacing )

Have a nice day:)

 

ldurniat



[TOPIC: post.html]
#4

dip

[GLOBAL: userInfoPane.html]
dip
  • Contributor

  • 142 posts
  • Corona SDK

Thanks guys, this is perfect really appreciate it.

 

Im just wondering now how I can stop colours of the same type from entering the chosenColors table as I am now getting 2 reds and 2 greens, which isn't what I want. 



[TOPIC: post.html]
#5

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,810 posts
  • Corona SDK

I would just shuffle your allColors table, and instead of taking random entries into chosenColors, take the first four.



[TOPIC: post.html]
#6

dip

[GLOBAL: userInfoPane.html]
dip
  • Contributor

  • 142 posts
  • Corona SDK

Oh as @davebollinger mentioned above. I was confused but do see how it works now and will give it a go.

 

Thanks for the help everyone :)



[TOPIC: post.html]
#7

Appletreeman

[GLOBAL: userInfoPane.html]
Appletreeman
  • Contributor

  • 444 posts
  • Corona SDK

2. shuffle allColors, then take the first 4 as chosenColors

 

Why oh why have I never heard of Rosettacode before???   I've only had a quick browse but have already found some really cool code :)

Thanks Dave :)



[TOPIC: post.html]
#8

dip

[GLOBAL: userInfoPane.html]
dip
  • Contributor

  • 142 posts
  • Corona SDK

Sorry to bump this thread again. Just stuck trying to figure out how to make the table objects in the loop unique. So each one uses the name etc. 

 

Ideally I would like the player to hit each object individually and I can make certain objects transition away etc. At the moment when I do a transition using 's' as the object it only takes the last object created. How can I make it so I can select any of the 4 I want? 

 

I hope that makes sense. Cheers



[TOPIC: post.html]
#9

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,347 posts
  • Enterprise

keep a reference to "s" inside chosenColors, fe

  chosenColors[i].s = s
  -- and remember to clean this up later


[TOPIC: post.html]
#10

dip

[GLOBAL: userInfoPane.html]
dip
  • Contributor

  • 142 posts
  • Corona SDK

It isn't working and Im sure its something to do with the way set it up or the way inserted my colours into the table. I will post it up so you guys can have a look when you get time but I will keep at it until then.

 while #choosenColors < 4 do -- *** INSERTS THE FIRST 4 COLOURS FROM ALLCOLOURS{} INTO CHOOSENCOLOURS{} *** -- 
        table.insert(choosenColors, allColors[1])
        table.insert(choosenColors, allColors[2])
        table.insert(choosenColors, allColors[3])
        table.insert(choosenColors, allColors[4])

        for i=1, #(choosenColors) do -- *** GETS THE DETAILS FROM THE TABLE AND DISPLAYS ITS PROPERTIES *** -- 
            idx = idx + 1
            s = display.newImageRect(choosenColors[i].image, choosenColors[i].height, choosenColors[i].width)
            s.x = (i-1)*65+60
            s.y = choosenColors[i].y
            s.name = choosenColors[i].name
            s:addEventListener("tap", revealColor)
            choosenColors[i].s = s
        end
    end

And then the function with the transition to which I want to manipulate them individually 

function newDelay( event )

 
    transition.to(choosenColors[1],{time=vanishTime, alpha=0.01, xScale = 0.1, yScale = 0.1}) 


[TOPIC: post.html]
#11

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 389 posts
  • Corona SDK

function newDelay( event )

    transition.to(choosenColors[1],{time=vanishTime, alpha=0.01, xScale = 0.1, yScale = 0.1}) 

You need reference to object. Use  choosenColors[1].s as first argument for transition e.g.

transition.to(choosenColors[1].s,{time=vanishTime, alpha=0.01, xScale = 0.1, yScale = 0.1}) 

Note:

  • You omitted local key word in definition of newDelay function so you make it global.
  • Consider make s variable local. 

 

Read more :

Good luck:)




[topic_controls]
[/topic_controls]