我使用SpriteKit(GameScene和GameViewController)和Objective-C创建了一款游戏。那里的一切都很好。 在同一个App中,我在故事板中创建了一个使用CollectionView的UIViewController和第二个viewController。我有collectionView加载数组。
我希望能够点击一个collectionView Cell并让它打开gameView。我能做什么。但是我想将一些信息从collectionView传递给GameScene,这样我就可以加载背景图像和其他信息来为每个collectionViewCell个性化GameScene。
这是我的collectionView
的代码#import "collectionViewController.h"
#import "GameScene.h"
#import "GameViewController.h"
@implementation collectionViewController
@synthesize animalArray,theImage,StringPicture,myCell;
-(void) viewDidLoad {
[super viewDidLoad];
animalArray = [NSArray arrayWithObjects:@"Cat.png",
@"image1.png",
@"image2.png",
@"image3.png",
,nil];
}
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return animalArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
UIImage *images;
long row = [indexPath row];
images = [UIImage imageNamed:animalArray[row]];
myCell.image1.image = images;
return myCell;
}
-(void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"indexPath.row:%ld",(long)indexPath.row);
if (indexPath.row==1) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"GameSceneOne"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
NSLog(@"mycell.image1.image:%@",myCell.image1.image);
[self presentViewController:vc animated:YES completion:NULL];
}
if (indexPath.row==2) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"GameSceneOne"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
}
}
我尝试将GameScene.image分配给vc.image,但其中一个是GameView,另一个是UIViewController,所以我不能这样做。
我尝试过使用segue但仍然无法将信息传递给另一个。
我该怎么做?
答案 0 :(得分:1)
我以不同的方式接近了这一点。基本上:
以下是代码:
第1步:
class GameCell: UICollectionViewCell {
@IBOutlet weak var myskview: SKView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
if let scene = SKScene(fileNamed: "MyScene") as? MyScene{
scene.scaleMode = .aspectFill
myskview.presentScene(scene)
}
}
}
第2步:
class MyScene: SKScene {
var gameLabel: SKLabelNode!
var monthValue: String?
var bird: SKSpriteNode?
var floor: SKSpriteNode?
var bgColor: UIColor?
let birdCategory: UInt32 = 0x1 << 0
let floorCategory: UInt32 = 0x1 << 1
override func didMove(to view: SKView) { }
第3步:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GameCell", for: indexPath) as! GameCell
let scene = cell.myskview.scene as! MyScene
scene.gameLabel.text = monthComponents[indexPath.row]
不要忘记使用以下方法在场景中创建标签:
gameLabel = scene?.childNode(withName: "gameLabel") as! SKLabelNode
您可以在我的git中查看最终结果:https://github.com/tennydesign/spriteKitAndCollectionView
请原谅我的尘土。 =)
答案 1 :(得分:0)
有很多方法可以做到这一点。其中一个是使用NSUserDefaults。
要在NSUserDefaults中存储数据,请执行以下操作:
CGRectMake(0, 0, 20, 20)
要阅读NSUserDefaults,请执行以下操作:
// object can be almost anything. Strings, arrays, dictionaries...
NSString *object = @"WellDone";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:object forKey:@"Hamburger"];
[defaults synchronize];
关键是您使用与创建时相同的密钥。
当然,请阅读docs(等等,等等,等等)。