-8

Okay, so I am copying a game that I have wrote in android studios over to swift for ios. I have figured out a lot of the syntax difference, but there is still some stuff that I cannot figure out. I will post the swift code first, then I will post the java code to compare with, and finally I will explain what is supposed to be happening:

func set(t: Int){
    _type = 0;  //For debugging otherwise equal to t
    delay = 0;
    current = 0;
    last = 0;
    count = 0;

    switch (_type){
    case 0:
        pt = Array<normal>();
        let p1 = normal(bpc: self, gameScene: gm);
        let p2 = normal(bpc: self, gameScene: gm);
        let p3 = normal(bpc: self, gameScene: gm);
        length = 3;   //for array length

        pt?.append(p1);
        pt?.append(p2);
        pt?.append(p3);

        hit = Array<Bool>();
        hit?.append(false);
        hit?.append(false);
        hit?.append(false);

        break;

Now for the equivalent java code which works:

void set(int t){
    _type = t;
    delay = 0;
    current = 0;
    last = 0;
    count = 0;

    switch (_type){
        case 0:
            pt = new normal[3];
            hit = new boolean[3];
            for (int i = 0; i < 3; i++){
                pt[i] = new normal();
                hit[i] = false;
            }
            break;

So The bottom code is the code in my android game that works. The code above is the code that I am trying to make work the same way, but I seem to be having problems with the array of object stuff. Also, I have the pt declared above as :

private var pt          : [Poop_Type]?;

and java equivalent is:

private Poop_Type pt[];
Zero
  • 67
  • 10
  • 1
    How about reading the https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/index.html book before? – Luca D'Alberti Apr 11 '16 at 07:10
  • alternatively, I'd probably be better off dropping a chunk of money to get game maker. I'm half tempted to just remove all dynamic objects, and just hard code everything so that everything is loaded and ran simultaneously and then use a very cpu intensive and error-prone if then section to choose which objects are to be updated, and have an affect. – Zero Apr 11 '16 at 07:16
  • 1
    If you're confused about optionals, and how to deal with them – my [answer here](http://stackoverflow.com/a/36360605/2976878) might be helpful. The Apple conceptual docs on Swift are amazingly clear and informative though, much better than any other programming language's docs I've seen. – Hamish Apr 11 '16 at 07:53
  • That actually is pretty helpful, but it still isn't solving my problem. My problem seems to mainly occur around using arrays of objects around optionals. Imagine a particle system, or bullet system, where the bullet type might change, or particle might change, so you have an array of a parent type, with different children types. When I am using the array with optionals (since I don't know what type might initially be used and since it can change) I run into problems. – Zero Apr 11 '16 at 08:05
  • I figured out the problem. That code actually worked. The problem was a conditional higher up that called down the chain wasn't working properly. – Zero Apr 12 '16 at 07:55

1 Answers1

0

I figured out the problem. That code actually worked. The problem was a conditional higher up that called down the chain that wasn't set.

Zero
  • 67
  • 10