How to call functions of a class member that is created under an array?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to call functions of a class member that is created under an array?

davidb05203
Hi, I am a student recently making my own project using jack language. I tried to create a simple breakout game. However, I'm stuck in a problem. I intended to create a series of blocks at the screen by a class named "Block", but i wanted to make them created in an Array blocks, so I can alternate or adjust them consistently. Thus I type down such code (as below)

var Array blocks;
while (index<numberOfBlocks){
    let blocks[index] = Block.new ( x, y, size);  // Block is a class creating the blocks
    let index=index+1;
}

The code successfully created a series of block. However, when I wanted to call a method under class Block, it failed by error

code:
var int position
while (index< numberOfBlocks){
    let position = blocks[index].aMethod();
    ...
}

error:
"In xxx.txt (line xx): In subroutine foo: Expected ;
 In xxx.txt (line xx): In subroutine foo: Expected statement(do, let, while, return or if)"

How can I reach the goal I wanted? Thanks for anyone who would answer this.
from a desperate student who is handling a project that should be done by a group :)))
Reply | Threaded
Open this post in threaded view
|

Re: How to call functions of a class member that is created under an array?

ivant
This post was updated on .
First, you don't have a semicolon at the end of "var int position" line.

If this is not the whole problem you can try to assign the array element to a variable of the right type and then use it for the call:
var int position;
var Block block;
while (index< numberOfBlocks){ 
    let block = blocks[index];
    let position = block.aMethod(); 
    ... 
} 

P.S. I don't have the time to dive into this more at the moment. But it's important to know if it's a real problem with Jack or just some minor syntax error. I think it's the former and I think the reason for this is that arrays in Jack don't really carry the type of their elements.

It's been some time now since I last used Jack, so I don't remember the details and I may be wrong. Please correct me, if that's the case.
Reply | Threaded
Open this post in threaded view
|

Re: How to call functions of a class member that is created under an array?

cadet1620
Administrator
Ivan's correct that in addition to the missing ';' you also need the temporary Array variable.

The syntax of Jack calls is a bit tricky, which makes them a bit difficult to use, but easier to compile when you get to chapter 10.

fun(...)
  calls method fun in the current class using the this variable. Only valid in constructors and methods.
a_var.fun(...)
  calls method fun in a_var's class.
A_Class.fun(...)
  calls function or constructor fun in class A_Class.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: How to call functions of a class member that is created under an array?

davidb05203
In reply to this post by ivant
    Wow that actually solve my problem! Your suggestion was right on spot!
    As you said, Jack language doesn't seem to carry the type of the object in an array, and it is reasonable to use class Block object to save the information.
    Thanks again for your helpful advice! :)
PS. the semicolon was my mistake on typing on the post, not the actual code. But thanks for the correction:)