I am working in xcode and developing an arcade like game. I can't seem to figure out how to move my character right only on the x-axis when the button labelled right is pressed. I have figured almost every other way to make the character move but with a button which makes this even more frustrating, and to most of you, this is probably a really simple answer. I'm writing in objective-c. If you could show me the code I would very much appreciate it.
2751 次
2 に答える
1
これは通常のフレームワークを使用しているだけで、cocos2dのようなものではないと思います。オブジェクトのフレームまたは中心を更新する必要があります!
-(void)buttonPressed:(id)sender {
CGRect frame = self.myObject.frame;
frame.origin.x += 20; //however many pixels to the right..
self.myObject.frame = frame;
}
于 2012-07-16T19:36:52.607 に答える
1
UIImageView、UIView、またはUIViewの他のサブクラスに表示されているキャラクターがある場合は、キャラクターを動かしたい距離だけフレームを更新するだけです。
- (IBAction)move {
int distanceRight = 5.0;
theView.frame = CGRectMake(theView.frame.origin.x + distanceRight, theView.frame.origin.y, theView.frame.size.width, theView.frame.size.height);
}
いいえ、View.frame 呼び出しを UIView アニメーション ブロックでラップして、ジャンプするだけでなく、新しい位置にアニメーション化することができます。
[UIView animateWithDuration:1.0f
animations:^{
theView.frame = CGRectMake(theView.frame.origin.x + distanceRight, theView.frame.origin.y, theView.frame.size.width, theView.frame.size.height);
}];
于 2012-07-16T19:40:00.277 に答える