Ios 如何从一系列按钮中连续随机发光一个UIButton
我正在做一个基于记忆的匹配益智游戏。为此,我需要从序列中随机发光按钮。大约一个星期以来,我一直在谷歌上搜索拼图、游戏和代码片段示例,但我无法找到合适的示例项目或源代码来开始 我通过改变背景图像来点亮按钮,我已经在故事板中为处于稳定状态的按钮彩色灯泡设置了默认图像。我使用了iOutletCollection的NSMutableArray,即tapLightButtons。以下是我试图通过随机模式获得的灯光序列,以获得游戏体验Ios 如何从一系列按钮中连续随机发光一个UIButton,ios,memory,puzzle,Ios,Memory,Puzzle,我正在做一个基于记忆的匹配益智游戏。为此,我需要从序列中随机发光按钮。大约一个星期以来,我一直在谷歌上搜索拼图、游戏和代码片段示例,但我无法找到合适的示例项目或源代码来开始 我通过改变背景图像来点亮按钮,我已经在故事板中为处于稳定状态的按钮彩色灯泡设置了默认图像。我使用了iOutletCollection的NSMutableArray,即tapLightButtons。以下是我试图通过随机模式获得的灯光序列,以获得游戏体验 -(void)startGlowingLights { [sel
-(void)startGlowingLights
{
[self shuffleValuesInArray:_tapLightButtons];
[self shuffleValuesInArray:lightArray];
[self shuffleValuesInArray:_initialButtonImages];
[self performSelector:@selector(animateButtonSequence) withObject:self afterDelay:0.2];
}
- (void) animateButtonSequence
{
__block UIButton *tapLight;
[UIView animateWithDuration:0.15
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
// button flash animation
} completion:^(BOOL finished) {
if (finished) {
if (i < _tapLightButtons.count) {
i++;
[self performSelector:@selector(animateButtonSequence) withObject:self afterDelay:1.0];
tapLight = [self.tapLightButtons objectAtIndex:i-1];
UIImage *glownLight = [UIImage imageNamed:[lightArray objectAtIndex:i-1]];
[tapLight setBackgroundImage:glownLight forState:UIControlStateNormal];
[[TTLMusicPlayer sharedInstance] playGlowLightSound:[[NSBundle mainBundle] pathForResource:@"beep" ofType: @"mp3"]];
//Reset the completed glowed button to previous state(off mode)
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if (i != 0 && i < _initialButtonImages.count) {
[tapLight setBackgroundImage:[self.initialButtonImages objectAtIndex:i-1] forState:UIControlStateNormal];
}
});
}
else {
i = 0;
}
NSLog(@"i value is %d",i);
}
}];
}
-(NSMutableArray *)shuffleValuesInArray:(NSMutableArray *)shuffledArray
{
for (NSInteger x=0;x<[shuffledArray count];x++)
{
NSInteger randomNumber = (arc4random() % ([shuffledArray count] - x)) + x;
[shuffledArray exchangeObjectAtIndex:x withObjectAtIndex:randomNumber];
}
return shuffledArray;
}
tapLightButtons是一个数组,其中包含困惑的按钮灯光,这些灯光以随机方式自动一个接一个地闪烁
lightArray是一个包含所有适当颜色图像的阵列,用于产生发光效果
initialButtonImages包含按钮关闭模式灯的所有默认初始图像的数组
有些人认为我可以部分实现我想要的,令人惊讶的是,即使在洗牌所有阵列之后,我仍然看到顺序的变化,因为发光的图像应该是stableoff模式彩色灯光的对应,然后在重置后,稳定的图像应该与灯光实际发光之前的图像匹配
也在Stack Exchange的游戏开发网站上发布了
游戏屏幕,以便更好地理解
彩光闪烁后
如何正确地从序列中随机地点亮按钮?我会用另一种方法来实现这一点,从而简化代码。我在IB中创建了一个包含9个自定义按钮的数组,并将它们添加到outlet集合中。我对按钮进行了子类化,并添加了一个flashWithOnTime:方法,因此按钮本身将负责在正常和高亮状态之间切换 这是button子类中的方法
-(void)flashWithOnTime:(CGFloat)onTime {
[self setHighlighted:YES];
[self performSelector:@selector(setHighlighted:) withObject:@NO afterDelay:onTime];
}
这是视图控制器中的代码,用于从按钮点击开始序列。我没有洗牌,而是从数组中随机选取一个索引,然后删除该条目,这样就不能再选取它了。ImageWithSolidColor和onImageWithSolidColor方法正是我用来创建图像的方法
@interface ViewController ()
@property (strong, nonatomic) IBOutletCollection(RDFlashingButton) NSArray *buttons;
@property (strong,nonatomic) NSArray *indexes;
@property (strong,nonatomic) NSMutableArray *mutableIndexes;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.indexes = @[@0,@1,@2,@3,@4,@5,@6,@7,@8];
for (RDFlashingButton *aButton in self.buttons) {
[aButton setBackgroundImage:[self offImageWithSolidColor] forState:UIControlStateNormal];;
[aButton setBackgroundImage:[self onImageWithSolidColor] forState:UIControlStateHighlighted];
}
}
-(void)flashRandomly {
if (self.mutableIndexes.count > 0) {
int index = arc4random_uniform(self.mutableIndexes.count);
int value = [self.mutableIndexes[index] intValue];
RDFlashingButton *aButton = self.buttons[value];
[aButton flashWithOnTime:0.4];
[self.mutableIndexes removeObjectAtIndex:index];
[self performSelector:@selector(flashRandomly) withObject:nil afterDelay:.4];
}
}
-(IBAction)startFlashing:(id)sender {
self.mutableIndexes = [self.indexes mutableCopy];
[self flashRandomly];
}
有时我想知道为什么人们会把整个想法放在公共论坛上,因为他们中的大多数人都是为某个客户做的,所以我看不出有什么违反NDA@amar请向我们提供更多信息,以便我们能够更好地帮助您,您想做什么,为什么要这样做等等。这是我经常收到的来自专家和版主的评论。对于您的评论,我已经以更具体的方式修改了我的问题,请现在检查:我不认为我能在短时间内给出具体的解决方案,但是一个指针子类UIButton可以保持与您的算法相关的额外状态,并查看它是否有帮助这不只是一个周期性地在某个范围内选择一个随机数的问题吗,1-n,其中n是按钮的数量,并点亮相应的按钮?关于随机数的选择,大约有一百万个问题。从序列中随机选择这意味着什么?我能够理解你答案的每一点,非常感谢,但我无法理解崩溃的原因:-[UIButton flashWithOnTime:]:未识别的选择器已发送到实例0x97a2bd0…请检查并确认我的实现是否正确,再次感谢:@EshwarChaitanya,您收到该错误,因为您没有将IB中部分或所有按钮的类更改为TTLFlashingButtons。错误是flashWithOnTime:消息被发送到UIButton,而不是您的子类。您的代码实现看起来不错。非常感谢,这很有帮助,但问题是按钮会发光,但只有一次……然后它会停止,而不是持续地停止,尽管我们正在删除索引以便重用,而且我们正在为操作添加延迟:@EshwarChaitanya,您希望发光的行为如何?您一次想要多个吗?你希望它是连续的吗?在你的问题中,你没有给出太多细节。如果我们不删除objectAtIndex,那么序列是连续的,这就是我想要的,非常感谢你的帮助。但我需要禁用4灯和5灯模式的角灯,如何做到这一点。我的猜测是,如果我们放置一个标签,如果用户点击同一个标签的灯,那么他就会进步,是这样吗?很抱歉再次打扰您,谢谢: