【问题标题】:passing info from collectionView to gameScene将信息从 collectionView 传递到 gameScene
【发布时间】:2015-06-16 21:29:37
【问题描述】:

我使用 SpriteKit(GameScene 和 GameViewController)和 Objective-C 创建了一个游戏。那里的一切都很好。 在同一个应用程序中,我在情节提要中创建了一个 UIViewController 和第二个使用 CollectionView 的 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,但信息仍然无法从另一个传递到另一个。

我应该怎么做?

【问题讨论】:

    标签: uiviewcontroller sprite-kit uicollectionview


    【解决方案1】:

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

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

    代码如下:

    第一步:

    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)
            }
        }
    }
    

    第二步:

    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) { }
    

    第三步:

    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

    请原谅我的灰尘。 =)

    【讨论】:

      【解决方案2】:

      有几种方法可以做到这一点。其中之一是使用 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(废话,废话,废话)。

      【讨论】:

      • 不知道为什么我没有想到这个。过于专注于另一个更难的解决方案。谢谢就像魅力一样。
      猜你喜欢
      • 2021-08-30
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多