passing info from collectionView to gameScene

2019-03-01 00:29发布

I have created a game using SpriteKit (GameScene and GameViewController) and Objective-C. Everything there works fine. Within the same App I have created a UIViewController and a second viewController in storyboard that uses CollectionView. I have the collectionView loading the array.

I want to be able to tap on a collectionView Cell and have it open the gameView. Which I can do. However I would like to pass some information from the collectionView to the GameScene so I can load the background image and other information to individualize the GameScene for each collectionViewCell.

Here is my code for the 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];

    }
}

I have tried assigning the GameScene.image to the vc.image but one is a GameView and the other is a UIViewController so I can't do that.

I have tried using a segue but still the information can't be passed from on to the other.

How should I do this?

2条回答
Animai°情兽
2楼-- · 2019-03-01 01:05

Couple of ways to do this. One of them is using NSUserDefaults.

To store data in NSUserDefaults, do this:

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

NSString *object = @"WellDone";

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

To read NSUserDefaults, do this:

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

The key being that you use the same key as when you created it.

And of course, read the docs (blah,blah, blah).

查看更多
Evening l夕情丶
3楼-- · 2019-03-01 01:18

I approached this in a different way. Basically:

  1. Cell has the skview and loads and presents scene.
  2. Scene exposes properties (like a label for the months in the example)
  3. In the cellForItemAt of the collectionView datasource, I instantiate the scene and just use the exposed properties.

Here is in code:

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

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

Step 3:

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GameCell", for: indexPath) as! GameCell
let scene = cell.myskview.scene as! MyScene
scene.gameLabel.text = monthComponents[indexPath.row]

Just don't forget to create the label inside the scene using:

gameLabel = scene?.childNode(withName: "gameLabel") as! SKLabelNode

You can check the final result in my git: https://github.com/tennydesign/spriteKitAndCollectionView

Just pardon my dust. =)

查看更多
登录 后发表回答