将信息从 collectionView 传递到 gameScene

passing info from collectionView to gameScene

我使用 SpriteKit(GameScene 和 GameViewController)和 Objective-C 创建了一个游戏。那里一切正常。 在同一个应用程序中,我在使用 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,但仍然无法将信息传递给另一个。

我应该怎么做?

有几种方法可以做到这一点。其中之一是使用 NSUserDefaults。

要将数据存储在 NSUserDefaults 中,请执行以下操作:

// object can be almost anything. Strings, arrays, dictionaries...

NSString *object = @"WellDone";

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:object forKey:@"Hamburger"];
[defaults synchronize];

要读取 NSUserDefaults,请执行以下操作:

NSString *myString = [[NSUserDefaults standardUserDefaults] objectForKey:@"Hamburger"];

关键是您使用的是与创建时相同的密钥。

当然,阅读 docs(废话,废话,废话)。

我以不同的方式处理这个问题。基本上:

  1. Cell 具有 skview 并加载和呈现场景。
  2. 场景公开属性(如示例中月份的标签)
  3. 在 collectionView 数据源的 cellForItemAt 中,我实例化场景并仅使用公开的属性。

代码如下:

第 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

请原谅我的粉尘。 =)