Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with drawing cards and getting them to center themselves based on how many cards there are?

Asked by 4 years ago
Edited 4 years ago

I'm trying to make a basic card game and want the cards to change position depending on how many cards there are like this: https://gyazo.com/a249f906ce3c77364101551f9a3113a4

In the gif whenever the user places a card somewhere else, the other cards they're holding move through the x axis to be symmetrical.

Right now my game draws 5 cards but when 1 card leaves the rest of the cards stay in their place. https://gyazo.com/98907c6df40faebdb5dae226339cbeb9

function FindChildren(Object)
    local c = game.Players.LocalPlayer.Backpack.Hand -- Counts how many cards in hand
    for i,v in pairs(Object:GetChildren()) do
        if v:IsA("Frame") and v.Name == "Card" and c.Value < 5 then -- Finds frame named card
        local d = 0.425 + (c.Value * 0.064) -- d is the distance between each card drawn
            c.Value = c.Value + 1 -- counts the card in a number value
            wait()
            v:TweenPosition(UDim2.new(d, 0, 0.822, 0), "Out", "Quint", 1.5, true)
        wait(0.5)
        end
        FindChildren(v)
    end
end

Any tips on what I could do to center the cards depending on the value in game.Players.LocalPlayer.Backpack.Hand ?

0
I think you may be able to achieve this using the UI Constraints (list layout maybe) ForeverBrown 356 — 4y
0
Hi thank you list layout achieved what I was trying to do. hydrolyte 14 — 4y

1 answer

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

WITH FRAME:

1) Have the cards inside a frame that is the size of the cards after they're layed out.

So the frames x size is of the num_of_cards * offset of each card * card's x size with the y being the cards y size.

2) position the frame in the center by making it's x position .5 - frame x size pretty sure.

Don't quote me on the exact numbers but that's how I think it could be done.

WITHOUT FRAME:

There's probably a way to get the offset without the for loop, but this works for now: It finds out where the last card should be without any centering, then it takes the distance away from the right edge, divides that by two to make the space on each edge even and subtracts half the size of the card to account for the card size.

template = script.Parent.cardTemplate
numCards = 5
offset = .01
totalXPos = 0

--determine the initial card's offset for centering in the cards middle
for i = 1, numCards do
    --looking at the last card, get it's position away from the right
    if(i == numCards)then
        totalXPos =((1-totalXPos)/2) - (template.Size.X.Scale/2)
    else
        totalXPos = totalXPos + template.Size.X.Scale + offset
    end
end

--create cards normally with initial card offset 
for i = 1, numCards do
    clone = template:Clone()
    clone.Parent = script.Parent.Cards
    clone.Position = UDim2.new(totalXPos,0,0,0)
    totalXPos = totalXPos + clone.Size.X.Scale + offset
end
template.Visible = false
0
I've thought about putting them all in a frame but the problem with that is that when I wont be able to tween the cards nicely on the field when they are played. hydrolyte 14 — 4y
0
Also have a number of cards variable which you change each time the player subtracts or adds to the deck. Would love to play Yu-Gi-Oh on Roblox royaltoe 5144 — 4y
0
Why can't you? royaltoe 5144 — 4y
0
I'll look into a solution without frames in a bit I have to commute to class currently on the train. royaltoe 5144 — 4y
View all comments (8 more)
0
Because when I change the parent of the card from the frame to the field roblox automatically starts tweening from the bottom left corner instead and makes it look weird. hydrolyte 14 — 4y
0
Made an edit to the post. Sorry I didn't get to work on it before my class started. royaltoe 5144 — 4y
0
but it's good now royaltoe 5144 — 4y
0
oh wow thanks a lot I'll look at this hydrolyte 14 — 4y
0
I think I misworded what I was trying to do. I'll try and find a better gif showing what I meant by centering. hydrolyte 14 — 4y
0
sure thing royaltoe 5144 — 4y
0
Are you asking if you want them tweened to the center card or do you want them tweened away from the center card after they group up? I'm not sure what you're asking royaltoe 5144 — 4y
0
i want the cards as a collective to tween back to the middle and replace any gaps when 1 of the cards is played hydrolyte 14 — 4y
Ad

Answer this question