08.11.2012 Views

Designing Games with Game Maker - YoYo Games

Designing Games with Game Maker - YoYo Games

Designing Games with Game Maker - YoYo Games

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

return (argument0*argument0);<br />

To call a script from <strong>with</strong>in a piece of code, just act the same way as when calling functions.<br />

That is, write the script name <strong>with</strong> the argument values in parentheses.<br />

With constructions<br />

As indicated before, it is possible to read and change the value of variables in other instances.<br />

But in a number of cases you want to do a lot more <strong>with</strong> other instances. For example,<br />

imagine that you want to move all balls 8 pixels down. You might think that this is achieved by<br />

the following piece of code<br />

ball.y = ball.y + 8;<br />

But this is not correct. The right side of the assignment gets the value of the y-coordinate of<br />

the first ball and adds 8 to it. Next this new value is set as y-coordinate of all balls. So the<br />

result is that all balls get the same y-coordinate. The statement<br />

ball.y += 8;<br />

will have exactly the same effect because it is simply an abbreviation of the first statement. So<br />

how do we achieve this? For this purpose there is the <strong>with</strong> statement. Its global form is<br />

<strong>with</strong> () <br />

indicates one or more instances. For this you can use an instance id, the name<br />

of an object (to indicate all instances of this object) or one of the special objects (all, self,<br />

other, noone). is now executed for each of the indicated instances, as if that<br />

instance is the current (self) instance. So, to move all balls 8 pixels down, you can type.<br />

<strong>with</strong> (ball) y += 8;<br />

If you want to execute multiple statements, put curly brackets around them. So for example,<br />

to move all balls to a random position, you can use

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!