Hans Hartle Hans Hartle - 2 years ago 78
Objective-C Question

How to move an object to a specific position in Objective C?

I'm currently in development of a card game of sorts, and when a user plays a card, I want that card to move from its current location to the "face up" pile on the screen. Currently, just to have a playable version, I have an object at that location that changes it's image based on which card has been played, but would look much cleaner with an animation from the hand to the deck.

I've done a bit of looking into layers and CAnimation and that looks some what promising, however I can't find a tutorial on how to move my "card" object in the manner I stated above, and all questions that I've found on this site similar to this topic contain outdated solutions. Not sure if there is a better way and I am pretty confused on this topic in general, so any help is greatly appreciated.

Edit: To answer some questions, and make the question/answers clean, here is some of my code.

This is my card class which stores all the information of the cards used by the AI to play the game (Not that important to this question). The class is NOT a UIView class, mainly due to the fact that I have never ventured into layers and CAnimation before this project.

+(id)card:(NSString*)suit :(int) value {
HHCard *card = [HHCard new];
card.value = value;
card.suit = suit;
//used for selection of cards during human turn
card.isSelected = false;
return card;

As to the other questions:

1. This is for mobile devices.

2. Since I currently am only using one view in this specific project so far, I don't know what my view hierarchy should look like to be efficient code wise.

3. So far I have mostly tried to achieve this effect with the brute force method using SKActions which I have yet to fully achieve. All of the tutorials I have tried to follow using KeyFrameAnimations have yet to work for me.

Answer Source

I apologize for not answering the question sooner, however the answer is quite simple. In order to move an object, in this example a 'card' to the desired location on the screen, create a CGPoint that contains the point where you wish your object to move, and then have your object run the SKAction moveTo:(CGPoint)duration:(NSTimeInterval).

CGPoint newPosition = {newX,newY};
[yourObject runAction:[SKAction moveTo:newPosition duration:lengthOfAnimation]];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download