Flipping over a Card

I'm trying to build a widget that looks like a playing card. When clicked I'd like it to change to look like the other side of the playing card. So far I have the back of the card, the cursor changes when I mouse over the card to the hand, but how do I then get the card to show the face image? Once flipped over how can I get it to flip back?
26,734 views 25 replies
Reply #1 Top
Oooo, I did one of these a long time ago when I first started with DX. Will have a look through my archives.

Are you using two images or an animated png flip?
Reply #2 Top
Two different images. I'm not certian how to do an animated .png flip yet. But I'd like to learn how.
Reply #3 Top
RomanDA did a really cool flashcard thing a while back.  Probably no help here though.  ;)
Reply #6 Top
That really works well. Now if I can just figure out how to do that for myself. I appreciate the pointer, Thanks.

Reply #7 Top
Okay. My animation was old and needs some work.

The simplest method is to create two states. Name the first state back and the second state front.
Then use this code to change them.
Code: vbscript
  1. 'Called when L-click is released
  2. Function Object_OnLButtonUp(x, y, dragged)
  3. If Not dragged Then
  4. If Object.State= "back" Then
  5. Object.State="front"
  6. ElseIf Object.State="front" Then
  7. Object.State="back"
  8. End If
  9. End If
  10. End Function
Reply #8 Top
OK, I understand the Front/Back portion, but where in the object do I specify what images are Front and Back?

Reply #9 Top
In the states section. Instead of selecting a pre-existing state. Type your own.

Like this:
Reply #10 Top
Hmmm, does it matter that I'm working as a shortcut? Do I need to be as a different object type? I cut and pasted your code into the script column and defined the "Front" and "Back" as directed. The front looks right but I'm unable to get it to look like the back. It does, however, execute the directed .exe command. On a different note...your Desktop X properties tool looks a lot different than mine.

Reply #11 Top
Never mind, it helps when you spell things exactly as specified. Works like a charm, Thanks.
Reply #12 Top
The above will give you an instant 'flip'. You could use an animated state to display a 'flipping' card image, but for a quick result which might work try this:

Sub animStateFlip(objName,newStateName)
If DesktopX.IsObject(objName) = False then Exit Sub

Set obj = DesktopX.Object(objName)
frames = 5
objLeftOriginal = obj.Left
objWidthOriginal = obj.Width
widthStep = objWidthOriginal/frames
For a = 1 to frames
newWidth = obj.Width - widthStep
obj.Width = newWidth
newLeft = obj.Left + widthStep/2
obj.Left = newLeft
Next
obj.Width = 0
obj.State = newStateName
For a = 1 to frames
newWidth = obj.Width + widthStep
obj.Width = newWidth
newLeft = obj.Left - widthStep/2
obj.Left = newLeft
Next
obj.Width = objWidthOriginal
obj.State = objLeftOriginal
End Sub

Call the function with the name of an object, and give it the state you want to change it to.
Increase the value of frames to make it slower and more smooth.

Could be adjusted in lots of ways.
Regards, Skarn
+1 Loading…
Reply #13 Top
Nice one Skarn!

Here's the code to use what I posted above with Skarn's flip script.
Code: vbscript
  1. Function Object_OnLButtonUp(x, y, dragged)
  2. If Not dragged Then
  3. If Object.State= "back" Then
  4. Call animStateFlip(Object.Name,"front")
  5. 'Object.State="front"
  6. ElseIf Object.State="front" Then
  7. Call animStateFlip(Object.Name,"back")
  8. 'Object.State="back"
  9. End If
  10. End If
  11. End Function
Reply #14 Top
/me loves collaboration!
Reply #15 Top
i was going to do a animated flip over on the Flash Card, but i wanted it faster than that.

Good suggestions all.
Reply #16 Top
Nice script you guys!
Reply #17 Top
i was going to do a animated flip over on the Flash Card, but i wanted it faster than that.Good suggestions all.


Maybe tray a Step Function with Object.Sleep. Messy but, it might be faster.

Here's one adapted for Top Flipping. You get another animation if you set your object width/height to "maintain aspect ratio".

Code: vbscript
  1. Function Object_OnLButtonUp(x, y, dragged)
  2. If Not dragged Then
  3. If Object.State= "back" Then
  4. Call animStateFlipH(Object.Name,"front")
  5. ElseIf Object.State="front" Then
  6. Call animStateFlipH(Object.Name,"back")
  7. End If
  8. End If
  9. End Function
  10. Sub animStateFlipH(objName,newStateName)
  11. If DesktopX.IsObject(objName) = False Then Exit Sub
  12. Set obj = DesktopX.Object(objName)
  13. frames = 5
  14. objTopOriginal = obj.Top
  15. objHeightOriginal = obj.Height
  16. heightStep = objHeightOriginal/frames
  17. For a = 1 To frames
  18. newHeight = obj.Height - heightStep
  19. obj.Height = newHeight
  20. newTop = obj.Top + heightStep/2
  21. obj.Top = newTop
  22. Next
  23. obj.Height = 0
  24. obj.State = newStateName
  25. For a = 1 To frames
  26. newHeight = obj.Height + heightStep
  27. obj.Height = newHeight
  28. newTop = obj.Top - heightStep/2
  29. obj.Top = newTop
  30. Next
  31. obj.Height = objHeightOriginal
  32. obj.State = objTopOriginal
  33. End Sub
Reply #18 Top
need to add these to the snippets on the new site..

http://testsite.desktopx.info
Reply #19 Top
reduce the number of frames to make it faster.

I don't think you could make it faster through any other code method, the way it's written it should do each change in width as fast as the computer possibly can.

You will note I don't have any obj.Width = obj.Width - widthAmount type commands, I found the double reference to an object in this way tended to be slower.

I am 'fairly' confident using a 'get property value to variable, adjust value of variable, reassign value to object property' was the fastest method.

SirSmiley, what is a step function?
Reply #20 Top
Oops, not a function but, a step counter of sorts. Can't remember where I put it but, it actually would be slower in this situation.

Used it for animation control. If memory serves correctly the meat of it was this:
Code: vbscript
  1. Step 1
  2. Object.SetPicture="1.png"
  3. Object.Sleep 1000
  4. Step 2
  5. Object.SetPicture="2.png"
  6. Object.Sleep 5000


I think I opted to not use this but, replaced with a couple of timers and array's. One array for images and the second for the timer.
Reply #21 Top

Hi I am new at all this but learning. I want to try to do a flipping image. I understand I must create two objects, call one front and the other back. Questions: in which object do i add karn's flip script? And could someone tell me exactly where to put the name front and back in the script? Last, after this is done, do I group both objects (normal, optimized or combined)?

thanks for your help

Reply #22 Top

You create one object with two states. See the picture in post nine.

You can rename states or enter a new state name when you click the add button.

Reply #23 Top

WeatherBound is watching and Learning.:borg:  

Thanks all:cylon:

 

Reply #24 Top

SirSmiley, thank you, I tried it and it works.

I would like to now try to make an image (object) that slides open and shows something like digital time. Would someone know where I might find a tutorial or help doing this?

thanks for your time and help.

Reply #25 Top

I would like to now try to make an image (object) that slides open and shows something like digital time. Would someone know where I might find a tutorial or help doing this?

 

There are PLENTY of tutorials out there on all things DX

Startdock DesktopX Documentation

Click on Tutorials

 

another thing, try google'ing things like DesktopX Sliding, or DesktopX Tutorials you might be amazed all the things you find.