Ball
Last Updated On 2021-07-16
Version 8MinSDK 7Tutorial Video
A round 'sprite' that can be placed on a Canvas
, where it can react to touches and drags, interact with other sprites (ImageSprite
s and other Ball
s) and the edge of the Canvas, and move according to its property values.
For example, to have a Ball
move 4 pixels toward the top of a Canvas
every 500 milliseconds (half second), you would set the Speed
property to 4 [pixels], the Interval
property to 500 [milliseconds], the Heading
property to 90 [degrees], and the Enabled
property to True
.
The difference between a Ball
and an ImageSprite
is that the latter can get its appearance from an image file, while a Ball
's appearance can be changed only by varying its PaintColor
and Radius
properties.
Properties
You can set the below properties in the UI part of the builder.
- Enabled - it's a boolean value which is set to true by default.
- Heading - it's a float value which is set to 0 by default.
- Interval - it's a non negative integer value which is set to 100 by default.
- OriginAtCenter - it's a boolean value which is set to false by default.
- PaintColor - it's a color value which is set to &hff000000 by default.
- Radius - it's a non negative integer value which is set to 5 by default.
- Speed - it's a float value which is set to 0.0 by default.
- Visible - it's a boolean value which is set to true by default.
- X - it's a float value which is set to 0.0 by default.
- Y - it's a float value which is set to 0.0 by default.
- Z - it's a float value which is set to 1.0 by default.
Block Properties
You can set the below blocks properties in the blocks section of the builder.
Enabled
Controls whether the Ball moves and can be interacted with through collisions, dragging, touching, and flinging.
Heading
Returns the Ball's heading in degrees above the positive x-axis. Zero degrees is toward the right of the screen; 90 degrees is toward the top of the screen.
Interval
The interval in milliseconds at which the Ball's position is updated. For example, if the interval is 50 and the speed is 10, then every 50 milliseconds the sprite will move 10 pixels in the heading direction.
PaintColor
The color of the Ball.
Radius
The distance from the edge of the Ball to its center.
Speed
The number of pixels that the Ball should move every interval, if enabled.
Visible
Whether the Ball is visible.
X
The horizontal coordinate of the Ball, increasing as the Ball moves right. If the property OriginAtCenter is true, the coordinate is for the center of the Ball; otherwise, it is for the leftmost point of the Ball.
Y
The vertical coordinate of the Ball, increasing as the Ball moves down. If the property OriginAtCenter is true, the coordinate is for the center of the Ball; otherwise, it is for the uppermost point of the Ball.
Z
How the `Ball` should be layered relative to other
Methods
You can set the below method blocks in the blocks section of the builder.
Bounce
Makes the Ball bounce, as if off a wall. For normal bouncing, the edge argument should be the one returned by EdgeReached.
CollidingWith
Indicates whether a collision has been registered between this Ball and the passed sprite (Ball or ImageSprite).
MoveIntoBounds
Moves the Ball back in bounds if part of it extends out of bounds, having no effect otherwise. If the Ball is too wide to fit on the canvas, this aligns the left side of the Ball with the left side of the canvas. If the Ball is too tall to fit on the canvas, this aligns the top side of the Ball with the top side of the canvas.
MoveTo
Sets the x and y coordinates of the Ball. If CenterAtOrigin is true, the center of the Ball will be placed here. Otherwise, the top left edge of the Ball will be placed at the specified coordinates.
PointInDirection
Sets the heading of the Ball toward the point with the coordinates (x, y).
PointTowards
Turns the Ball to point towards a designated target sprite (Ball or ImageSprite). The new heading will be parallel to the line joining the centerpoints of the two sprites.
Events
You can set the below event blocks in the blocks section of the builder.
CollidedWith
Event handler called when two enabled sprites (
Dragged
Event handler called when a Ball is dragged. On all calls, the starting coordinates are where the screen was first touched, and the "current" coordinates describe the endpoint of the current line segment. On the first call within a given drag, the "previous" coordinates are the same as the starting coordinates; subsequently, they are the "current" coordinates from the prior call. Note that the Ball won't actually move anywhere in response to the Dragged event unless MoveTo is explicitly called. For smooth movement, each of its coordinates should be set to the sum of its initial value and the difference between its current and previous values.
EdgeReached
Event handler called when the Ball reaches an edge of the screen. If Bounce is then called with that edge, the Ball will appear to bounce off of the edge it reached. Edge here is represented as an integer that indicates one of eight directions north (1), northeast (2), east (3), southeast (4), south (-1), southwest (-2), west (-3), and northwest (-4).
Flung
Event handler called when a fling gesture (quick swipe) is made on an enabled Ball. This provides the x and y coordinates of the start of the fling (relative to the upper left of the canvas), the speed (pixels per millisecond), the heading (0-360 degrees), and the x and y velocity components of the fling's vector.
NoLongerCollidingWith
Event handler called when a pair of sprites (Balls and ImageSprites) are no longer colliding.
TouchDown
Event handler called when the user begins touching an enabled Ball (placing their finger on a Ball and leaving it there). This provides the x and y coordinates of the touch, relative to the upper left of the canvas.
TouchUp
Event handler called when the user stops touching an enabled Ball (lifting their finger after a TouchDown event). This provides the x and y coordinates of the touch, relative to the upper left of the canvas.
Touched
Event handler called when the user touches an enabled Ball and then immediately lifts their finger. The provided x and y coordinates are relative to the upper left of the canvas.