- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一款游戏使用全局整数 position
在选项菜单中导航。当为每个屏幕按下后退按钮时,它会调用函数 [self options]
以重新加载原始“选项”屏幕。我觉得这是我搞砸的非常愚蠢的事情。
现在,调用判断是否返回按钮的函数的代码是这样的:
if ([self touchIsInNode:[self childNodeWithName:@"back"] touchPoint:touchPoint]) {
// stuff should happen here
}
touchIsInNode:
是我自己的自定义方法,用于处理返回 BOOL
值的点击(如果是,则为 YES
)。
将每个屏幕的后退按钮命名为 backa
、backb
、backc
等是一种可行的解决方法,但它仍然无论 position
的值如何,都会在每个 switch case 中调用代码。
这是我在 switch
语句中进行的操作:
switch (position) {
// case 0 thru 3 are unrelated to the question... 0 is for the main menu, 1 is for the original logic for the "options" screen, 2 is for the end of the game, and 3 is to skip to the end of the intro screen.
case 4:
{
NSLog(@"position: %i", position);
if ([self touchIsInNode:[self childNodeWithName:@"backa"] touchPoint:touchPoint]) {
NSLog(@"store back button was pressed and [self options] is being called...");
[self options];
}
// store UI
}
case 5:
{
NSLog(@"position: %i", position);
if ([self touchIsInNode:[self childNodeWithName:@"backb"] touchPoint:touchPoint]) {
NSLog(@"stats back button was pressed and [self options] is being called...");
[self options];
}
// stats UI
}
case 6:
{
NSLog(@"position: %i", position);
if ([self touchIsInNode:[self childNodeWithName:@"backc"] touchPoint:touchPoint]) {
NSLog(@"about back button was pressed and [self options] is being called...");
[self options];
}
// about UI
}
case 7:
{
NSLog(@"position: %i", position);
if ([self touchIsInNode:[self childNodeWithName:@"backe"] touchPoint:touchPoint]) {
NSLog(@"dev options back button was pressed and [self options] is being called...");
[self options];
}
// dev options UI
}
case 8:
{
NSLog(@"position: %i", position);
if ([self touchIsInNode:[self childNodeWithName:@"backd"] touchPoint:touchPoint]) {
NSLog(@"purchased options back button was pressed and [self options] is being pressed...");
[self options];
}
// purchased options UI
}
default:
{
NSLog(@"INVALID POSITION: %i", position);
break;
}
}
}
我的 options
方法如下所示:
for (SKNode* node in [self children]) {
// fade out and remove each node if it is not "optionsButton"
if (![node.name isEqual:@"optionsButton"]) {
[node runAction:[SKAction fadeAlphaTo:0 duration:1] completion:^{
[node removeFromParent];
}];
} else {
// if it is "optionsButton", perform an animation.
[node runAction:[SKAction scaleTo:1.5 duration:1]];
[node runAction:[SKAction moveTo:CGPointMake(self.size.width / 2, self.size.height - ((node.frame.size.height * (4.0 / 3.0)) / node.yScale)) duration:1] completion:^{
// afterwards, call a method that logs each node's name.
[self logEveryNode];
}];
}
}
position = 1;
NSLog(@"position: %i", position);
// processing labels
// add in each child
NSLog(@"options called, with %i nodes in [self children]", [self children].count);
// fade in all of the labels
然后,对于选项菜单中的每个子菜单,它们看起来像这样(使用“商店”作为模型):
position = 4;
for (SKNode* node in [self children]) {
if ([node.name isEqual:@"optionsButton"]) {
[node runAction:[SKAction scaleTo:(2.0 / 3.0) duration:1]];
[node runAction:[SKAction moveTo:CGPointMake(self.size.width / 2, self.size.height - (node.frame.size.height * (1.0 / 3.0))) duration:1]];
} else if ([node.name isEqual:@"store"]) {
[node runAction:[SKAction scaleTo:1.5 duration:1]];
[node runAction:[SKAction moveTo:CGPointMake(self.size.width / 2, self.size.height - node.frame.size.height * 3) duration:1]];
} else {
[node runAction:[SKAction fadeAlphaTo:0 duration:1] completion:^{
[node removeFromParent];
}];
}
}
// draw UI for the store and move the back button accordingly
SKLabelNode *back = [SKLabelNode labelNodeWithFontNamed:@"Cochin"];
back.alpha = 0;
back.fontColor = [UIColor blackColor];
back.text = @"back";
back.name = @"backa";
back.fontSize = 44;
back.position = CGPointMake(self.size.width / 2, self.size.height * 0.5);
[self addChild:back];
[back runAction:[SKAction fadeAlphaTo:1 duration:1]];
此代码的结果是,即使在 position
的值已更改为 1
以表示返回到 options< 之后,每个 case 都会执行
菜单。此外,didBeginTouches
被奇怪地调用了 5 次,尽管我只在模拟器中“点击”了一次屏幕。以下是我所拥有的日志中更相关的部分:
2015-05-23 01:10:40.581 game[25258:1981445] position: 4
2015-05-23 01:10:40.581 game[25258:1981445] store back button was pressed and [self options] is being called...
2015-05-23 01:10:40.582 game[25258:1981445] position: 1
2015-05-23 01:10:40.588 game[25258:1981445] options called, with 10 nodes in [self children]
2015-05-23 01:10:40.588 game[25258:1981445] position: 1
2015-05-23 01:10:40.588 game[25258:1981445] position: 1
2015-05-23 01:10:40.588 game[25258:1981445] position: 1
2015-05-23 01:10:40.589 game[25258:1981445] position: 1
2015-05-23 01:10:40.589 game[25258:1981445] INVALID POSITION: 1
为什么每个 case
都会运行,甚至是 default
case?似乎只有 position == 4
应该运行 case 4
而类似 position == 500
的东西会运行 default
案例。如果有帮助,选项菜单中有 5 个子菜单。
最佳答案
您需要在每个 case block 的末尾添加一个 break 语句。
关于ios - Objective-C switch 语句的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30409266/
我遵循了一本名为“Sitepoint Full Stack Javascript with MEAN”的书中的教程,我刚刚完成了第 6 章,应该已经创建了一个带有“数据库”的“服务器”。数据库只不过是
在 Jquery 中,我创建两个数组,一个嵌入另一个数组,就像这样...... arrayOne = [{name:'a',value:1}, {name:'b',value:2}] var arra
这个问题在这里已经有了答案: What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wa
我被放在别人的代码上,有一个类用作其他组件的基础。当我尝试 ng serve --aot(或 build --prod)时,我得到以下信息。 @Component({ ...,
我正在测试一些代码,并使用数据创建了一个 json 文件。 问题是我在警报中收到“[object Object],[object Object]”。没有数据。 我做错了什么? 这是代码:
我想打印 [object Object],[object Object] 以明智地 "[[{ 'x': '1', 'y': '0' }, { 'x': '2', 'y': '1' }]]"; 在 ja
我有一个功能 View ,我正在尝试以特殊格式的方式输出。但我无法让列表功能正常工作。 我得到的唯一返回是[object Object][object Object] [object Object]
在使用优秀的 Sim.js 和 Three.js 库处理 WebGL 项目时,我偶然发现了下一个问题: 一路走来,它使用了 THREE.Ray 的下一个构造函数: var ray = new THRE
我正在使用 Material UI 进行多重选择。这是我的代码。 {listStates.map(col => (
我的代码使用ajax: $("#keyword").keyup(function() { var keyword = $("#keyword").val(); if (keyword.
我遇到了下一个错误,无法理解如何解决它。 Can't resolve all parameters for AuthenticationService: ([object Object], ?, [o
我正在尝试创建一个显示动态复选框的表单,至少应选中其中一个才能继续。我还需要获取一组选中的复选框。 这是组件的代码: import { Component, OnInit } from '@angul
我正在开发 NodeJs 应用程序,它是博客应用程序。我使用了快速验证器,我尝试在 UI 端使用快速闪存消息将帖子保存在数据库中之前使用闪存消息验证数据,我成功地将数据保存在数据库中,但在提交表单后消
我知道有些人问了同样的问题并得到了解答。我已经查看了所有这些,但仍然无法解决我的问题。我有一个 jquery snipet,它将值发送到处理程序,处理程序处理来自 JS 的值并将数据作为 JSON 数
我继承了一个非常草率的项目,我的任务是解释为什么它不好。我注意到他们在整个代码中都进行了这样的比较 (IQueryable).FirstOrDefault(x => x.Facility == fac
我只是在删除数组中的对象时偶然发现了这一点。 代码如下: friends = []; friends.push( { a: 'Nexus', b: 'Muffi
这两个代码片段有什么区别: object = nil; [object release] 对比 [object release]; object = nil; 哪个是最佳实践? 最佳答案 object
我应该为其他人将从中继承的第一个父对象传递哪个参数,哪个参数更有效 Object.create(Object.prototype) Object.create(Object) Object.creat
我在不同的对象上安排不同的选择器 [self performSelector:@selector(doSmth) withObject:objectA afterDelay:1]; [self per
NSLog(@"%p", &object); 和 NSLog(@"%p", object); 有什么区别? 两者似乎都打印出一个内存地址,但我不确定哪个是对象的实际内存地址。 最佳答案 这就是我喜欢的
我是一名优秀的程序员,十分优秀!