我有一个SKSpriteNode(monsterNode)的子类.它会使用矢量自动在屏幕上运行以跟随播放器.我目前正在使用以下操作来使其运行:
SKAction *actionMove = [SKAction moveTo:actualDistance duration:time];
[self runAction:actionMove completion:^ {
_currentState = SVGMonsterStateIdle;
}];
Run Code Online (Sandbox Code Playgroud)
我想知道是否有可能使它如此怪物节点实际上STOPS运行动作,如果它击中iOS设备屏幕的边界.我目前在屏幕边缘有SKSpriteNode边界,与联系代表链接,以通知怪物和墙壁是否接触.但是,如果我无法阻止怪物的动作动作从完成开始,那就没有任何意义.怪物需要停在屏幕的边界.如果无法在执行中停止SKAction,是否有迂回的方法呢?
我一直在尝试UIGestureRecognizers 和新SKScene/SKNode's的SpriteKit.我有一个问题,我接近修复它但我对一件事感到困惑.基本上,我有一个平移手势识别器,允许用户在屏幕上拖动精灵.
我遇到的唯一问题是,实际初始化平移手势需要一次点击,然后只有在SECOND上点击它才能正常工作.我想这是因为我的平移手势已经初始化了touchesBegan.但是,我不知道在哪里放置它,因为在SKScene的initWithSize方法中初始化它会使手势识别器停止实际工作.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (!self.pan) {
self.pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(dragPlayer:)];
self.pan.minimumNumberOfTouches = 1;
self.pan.delegate = self;
[self.view addGestureRecognizer:self.pan];
}
}
-(void)dragPlayer: (UIPanGestureRecognizer *)gesture {
CGPoint trans = [gesture translationInView:self.view];
SKAction *moveAction = [SKAction moveByX:trans.x y:-trans.y duration:0];
[self.player runAction:move];
[gesture setTranslation:CGPointMake(0, 0) inView:self.view];
}
Run Code Online (Sandbox Code Playgroud) 我试图让它变成这样一个粒子,只要玩家被移动,我就会跟随玩家.我试图复制的效果就像你在网站上,并且他们有一些跟随你的鼠标的对象.我尝试通过使粒子移动玩家所做的数量来做到这一点,但它没有再现预期的效果.有什么建议?我的代码:
声明粒子
NSString *myParticlePath = [[NSBundle mainBundle] pathForResource:@"trail" ofType:@"sks"];
self.trailParticle = [NSKeyedUnarchiver unarchiveObjectWithFile:myParticlePath];
self.trailParticle.position = CGPointMake(0,0);
[self.player addChild:self.trailParticle];
Run Code Online (Sandbox Code Playgroud)
移动方法
-(void)dragPlayer: (UIPanGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateChanged) {
//Get the (x,y) translation coordinate
CGPoint translation = [gesture translationInView:self.view];
//Move by -y because moving positive is right and down, we want right and up
//so that we can match the user's drag location (SKView rectangle y is opp UIView)
CGPoint newLocation = CGPointMake(self.player.position.x + translation.x, self.player.position.y - translation.y);
CGPoint newLocPart = …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
-(void)aggravatedSeek:(SKSpriteNode *)target{
_isAggravated = YES;
SKAction *action = [SKAction runBlock:^{
double randTime = 0.2;
NSLog(@"%f", randTime);
[self moveToSpriteNode:target withTimeInterval:randTime];
}];
//SKAction *repeatAction = [SKAction repeatAction:action count:6];
SKAction *rep = [SKAction sequence:@[action, action, action, action, action]]; //Test Sequence
[self runAction:rep completion:^{
[self runAction:blackColorAction];
_isAggravated = NO;
}];
}
Run Code Online (Sandbox Code Playgroud)
我希望我的行动能够等到上一个动作完成.意思是,我的动作需要进行排序,以便动作在对象完成移动之前rep不会重复action动作.目前,似乎输出立即运行所有操作,并且不会像它应该那样对它们进行排序.我已经阅读了文档,我现在所拥有的是他们所说的......
由于某种原因,我的计时器只在前台运行.我在这里寻找解决方案,但我找不到任何好的解决方案.我的计时器使用Core Data,每次减量后都会保存timeInterval.此外,我发送UILocalNotification,但这不起作用.我假设它不发送警报视图,如果它在前台..这就是我现在拥有的:
-(IBAction)startTimer:(id)sender{
if (timer == nil) {
[startButton setTitle:@"Pause" forState:UIControlStateNormal];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
} else {
[startButton setTitle:@"Resume" forState:UIControlStateNormal];
[timer invalidate];
timer = nil;
}
}
-(void)timerAction:(NSTimer *)t
{
if(testTask.timeInterval == 0)
{
if (self.timer)
{
[self timerExpired];
[self.timer invalidate];
self.timer = nil;
}
}
else
{
testTask.timeInterval--;
NSError *error;
if (![self.context save:&error]) {
NSLog(@"couldn't save: %@", [error localizedDescription]);
}
}
NSUInteger seconds = (NSUInteger)round(testTask.timeInterval);
NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u",
seconds / 3600, (seconds …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过在进入后台时将时间保存到磁盘并在进入前台时检索它来让我的计时器在后台"运行".每个视图控制器都有一个计时器和用户指定的timeInterval.问题是,我不知道如何访问timeInterval变量.我想我可以通过使用这样的东西来获得时间的差异(这会起作用吗?):
NSTimeInterval idleTime = [dateReturnedToForeground timeIntervalSinceDate:dateEnteredBackground];
NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:startDate];
elapsedTime -= idleTime;
Run Code Online (Sandbox Code Playgroud)
每个视图控制器(和计时器/时间间隔)的访问方式如下:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailVC;
if (![self.detailViewsDictionary.allKeys containsObject:indexPath]){
detailVC = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
[self.detailViewsDictionary setObject:detailVC forKey:indexPath];
detailVC.context = self.managedObjectContext;
}else{
detailVC = self.detailViewsDictionary[indexPath];
}
Tasks *task = [[self fetchedResultsController] objectAtIndexPath:indexPath];
detailVC.testTask = task;
[[self navigationController] pushViewController:detailVC animated:YES];
NSLog(@"%@", self.detailViewsDictionary);
[[NSNotificationCenter defaultCenter] addObserver:detailVC forKeyPath:self.detailViewsDictionary[indexPath] options:nil context:nil];
}
Run Code Online (Sandbox Code Playgroud)
我将每个视图控制器添加到通知中心,以便可以在应用程序委托中访问它(我认为这是正确的?).问题是,我不知道如何将第一个代码与视图控制器代码结合起来,因为我无法在app delegate中访问视图控制器的属性.任何建议,以便我可以让我的计时器在后台"运行"?
所以我有一个Task实体类(Core Data),我试图覆盖其中一个字符串(timeIntervalString)的setter,因此它可以显示在表视图单元格的详细文本标签中.出于某种原因,我得到一个像这样的EXC_BAD_ACCESS错误:

[Tasks timeIntervalString]一直持续到37355 ...
这是我的代码:
-(NSString *)timeIntervalString{
NSUInteger seconds = (NSUInteger)round(self.timeInterval);
if ((seconds/3600) == 0){
if (((seconds/60) % 60) == 1) {
self.timeIntervalString = [NSString stringWithFormat:@"%u MIN", ((seconds/60) % 60)];
} else {
self.timeIntervalString = [NSString stringWithFormat:@"%u MINS", ((seconds/60) % 60)];
}
} else if ([self.conversionInfo hour] == 1) {
if (((seconds/60) % 60) == 0){
self.timeIntervalString = [NSString stringWithFormat:@"%u HR", (seconds/3600)];
} else if (((seconds/60) % 60) == 1) {
self.timeIntervalString = [NSString stringWithFormat:@"%u HR %u MIN", (seconds/3600), …Run Code Online (Sandbox Code Playgroud) 我不知道发生了什么.我试图通过比较日期来制作一个有效的计时器.当我启动计时器时,它有时会工作,然后随机停止,有时它只返回timeInterval的值.当时间间隔为负时,它似乎只能正常工作.这是我的方法:
-(IBAction)startTimer:(id)sender{
if (timer == nil) {
[startButton setTitle:@"Start" forState:UIControlStateNormal];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
date = [NSDate dateWithTimeIntervalSinceNow:testTask.timeInterval]; //instance variable
} else {
[startButton setTitle:@"Stop" forState:UIControlStateNormal];
[timer invalidate];
timer = nil;
}
}
-(void)timerAction:(NSTimer *)t
{
NSDate *currentDate = [NSDate date];
NSTimeInterval updatedTimeInterval = [date timeIntervalSinceDate:currentDate];
if (updatedTimeInterval > 0){
if (self.timer)
{
[self timerExpired];
[self.timer invalidate];
self.timer = nil;
}
}
else
{
testTask.timeInterval = updatedTimeInterval;
NSLog(@"%.2f", testTask.timeInterval);
NSError *error;
if (![self.context save:&error]) {
NSLog(@"couldn't …Run Code Online (Sandbox Code Playgroud) 假设您有以下方法:
CGPoint vectorSubtraction(CGPoint a, CGPoint b) {
return CGPointMake(a.x - b.x, a.y - b.y);
}
Run Code Online (Sandbox Code Playgroud)
你使用以下几点:
Point A: (1,4)
Point B: (10,3)
Run Code Online (Sandbox Code Playgroud)
如果您使用该vectorSubtraction方法,您会得到:
A POINT at (9,-1).
现在,我会理解是否(9,-1)是从第一个点到第二个点的向量(并且它是),但我不明白为什么向量本身是一个POINT.根据定义,矢量具有方向和幅度.一个点怎么能有方向和幅度?
objective-c ×8
ios ×7
sprite-kit ×4
iphone ×3
nstimer ×3
skaction ×2
background ×1
cgpoint ×1
countdown ×1
math ×1
nsstring ×1
particles ×1
sknode ×1
vector ×1