[英]iPhone, Cocos2D - moving sprite left/right while touching screen
I'm new to Objective C and app development so please go easy on me! 我是Objective C和应用程序开发的新手,所以请放轻松! I'm trying to make a basic game and need to move a sprite left or right continuously while the user's finger is on the screen - left side to go left, right to go right... I'm trying to use update to repeat movements of a few pixels every 1/60th second. 我正在尝试制作基本游戏,并且需要在用户的手指在屏幕上的同时连续向左或向右移动精灵-左侧向左移动,右侧向右移动...我试图使用更新重复每1/60秒移动几像素。 So far, this is what I have (and sorry about the formatting): 到目前为止,这就是我所拥有的(并且对格式感到抱歉):
#import "GameplayLayer.h"
@implementation GameplayLayer
-(id)init {
self = [super init];
if (self != nil) {
CGSize screenSize = [CCDirector sharedDirector].winSize;
// enable touches
self.isTouchEnabled = YES;
blobSprite = [CCSprite spriteWithFile:@"blob.png"];
[blobSprite setPosition: CGPointMake(screenSize.width/2, screenSize.height*0.17f)];
ball = [CCSprite spriteWithFile:@"ball.png"];
[ball setPosition:CGPointMake(10, screenSize.height*0.75f)];
[self addChild:blobSprite];
[self addChild:ball];
[self schedule:@selector(update) interval:1.0f/60.0f];
}
return self;
}
-(void) update:(ccTime)dt{
if (_tapDownLeft == YES){
blobSprite.position.x==blobSprite.position.x-5;
}
if (_tapDownRight == YES){
blobSprite.position.x==blobSprite.position.x+5;
}
}
-(void) ccTouchesBegan:(UITouch*)touch withEvent: (UIEvent *)event{
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
if (touchLocation.x > 400) {
if ((blobSprite.position.x+10)<460){
_tapDownRight = YES;
}
}
if (touchLocation.x < 200) {
if ((blobSprite.position.x-10>20)){
_tapDownLeft = YES;
}
}
else {
_tapDownLeft = NO;
_tapDownRight = NO;
}
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
_tapDownLeft = NO;
_tapDownRight = NO;
}
-(void) registerWithTouchDispatcher{
[[CCTouchDispatcher sharedDispatcher]addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
@end
Am I on the right lines with this? 我对此是否正确? At the moment it's giving me 'expression result unused' in update. 目前,它在更新中给了我“未使用的表达式结果”。 Could anyone tell me what I'm missing? 谁能告诉我我想念的东西吗? Any help would be greatly appreciated. 任何帮助将不胜感激。
Thanks, Patrick 谢谢,帕特里克
i see a few things here: 我在这里看到一些东西:
删除更新函数中的参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.