Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private object variables accessible from outside. #46

Open
ftvs opened this issue Apr 15, 2012 · 2 comments
Open

Private object variables accessible from outside. #46

ftvs opened this issue Apr 15, 2012 · 2 comments

Comments

@ftvs
Copy link

ftvs commented Apr 15, 2012

Hi ippa, here are a few things I noticed. I could open new issues for each if you want.

Private variables
Assigning to a rect's x/y does not alter right/bottom. This creates problems like tile_map.atRect(rect) not working due to right/bottom being off. I imagine x/y are supposed to be private. Is it possible for jaws' current design to accomodate private object member variables?

I worked around it by calling moveTo() every update() with the rect's current x/y.

Jumping mid air after walking off a ledge
Example 9 (the side scrolling platformer) uses the can_jump variable, which is assigned true when the player collides with the ground. The player is therefore able to jump in midair after falling from a ledge, because can_jump is still true.

I added a feet_rect under the player that "attaches" to his feet. I then added a canJump() function to check if the feet_rect is colliding with something. So the player only jumps when his feet are in contact with something.

player.canJump = function() {
  var collided_block = tile_map.atRect(feet_rect)[0];

  if (collided_block) {
    return true;
  }
}

// in the update function:
//if(jaws.pressed("up"))    { if(player.can_jump) { player.vy = -10; player.can_jump = false } }

if (jaws.pressed("up")) {
  if (player.canJump()) {
    player.vy = -10;
  }
}

Jaws side scrolling platformer example 9 "fixed"

Example 9 JavaScript gist "fixed"

Rect.js function parameters
Rect.js uses raw function parameters instead of an option object like most of the other classes. I'm not sure why this is the case.

Examples index.html
Being able to navigate the examples would be convenient. Maybe a link back to the index from each example, or even a table of contents.

Offline development
Chrome offline development is hairy due to XSS. Firefox 11 is happy to run them offline though. I'm not sure what can be done about this besides informing devs how to work around the inconvenience.

Tell me if anything is unclear or if you need more info. If none of these are of concern then I'll leave them be.

Thanks!

@ippa
Copy link
Owner

ippa commented Apr 17, 2012

Thanks for the detailed feedback! You make several good points.

Private variables
The private variables x/y was a major (and hard) design decision for me. I wanted the speed of direct properties. Especially for something you might modify a lot like x/y. The problem is, as you point out, other things that depend on x/y also needs to be updated. I was thinking of using the new setters/getters but skipped them cause of not enough support + not as fast as normal properties.

To a small degree Sprite has the same "problems". Where it's possible to directly do for example "sprite.image = an_image;" but the recommended way is using sprite.setImage(an_image) since then jaws can run code to recalc the bounding box and so on.

What I ended up with in the Sprite-constructor was Both ways. There's (chainable) setters for everything. But you could also go low-level and modify for example x and y directly if you want. I haven't decided yet if I should dump the priv var access and go 100% with chainable setters. Or at least rename all private variables, so it's sprite._x = 100 instead sprite.x = 100 .. as an extra security measure but also point out "Now you're modifying something private!"

Jumping mid air after walking off a ledge
This could be considered a bug, but also a feature ;). I've found in my pixel-perfect games (see www.ippa.se) that it's actually pretty fun being able to jump midair. It makes for some "I'll never make this jump... ooh I made it"-moments.

Rect.js function parameters
True. I don't wan't to break backwards compatibility. It's probably possible to support both the old x,y,width,height and a new object-argument though... pull-requests are welcome ;).

Examples index.html
Good idea, I'll see what I can do here.

Offline development
Right.. I have no ideas how to solve this now though.

@ftvs
Copy link
Author

ftvs commented Apr 27, 2012

Thanks for the response, ippa. That clarified things.

sprite._x sounds like a decent idea. At least that communicates somewhat that it's supposed to be private.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants