Sprite scan

Post Reply
Ken
Posts: 29
Joined: Tue Jan 29, 2019 1:49 am
My devices: Ipad
Location: NSW, Australia

Sprite scan

Post by Ken »

I am just starting with sprites. I started with the sprite scan command I typed the following code in.

x=20!y=500!w= 800!h=300
n$="sprite1"
SPRITE N$ SCAN x,y, w,h
sprite n$ show

This produces a black rectangle at 0,0 with whatever w and h I type in. Always at 0,0 no matter which x,y I use.

Why doesn't it appear at the x,y I type in?
How do I change the color?
Can I draw rectangles, buttons etc on it?

matt7
Posts: 115
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone
Location: USA

Re: Sprite scan

Post by matt7 »

Why doesn't it appear at the x,y I type in?
The reason why your sprite is always appearing at 0, 0 is because that is the default sprite position. Your x, y coordinates are telling the sprite scan command where on the screen to scan. That scan is put into the sprite that is located (by deafult) at 0, 0. You need to move the sprite after creating it if you want it to appear elsewhere.

Here I've added a line of code that moves the sprite to the same x, y it was scanned from:

Code: Select all

x=20 ! y=500 ! w=800 ! h=300
n$="sprite1"
sprite n$ scan x,y, w,h
sprite n$ at x, y
sprite n$ show

How do I change the color?
What is happening in your program (assuming no other lines of code before or after what you shared) is that you are scanning the graphics screen (which by default is solid black) to the sprite and then showing the sprite while the program is in TEXT mode (the default mode programs start in, which is solid white).

To better see this, switch to GRAPHICS mode and change the screen color in between scanning and showing the sprite:

Code: Select all

graphics
graphics clear 1, 0.5, 0.5    ' make the entire screen red (this will end up being scanned to the sprite)

x=20 ! y=500 ! w=800 ! h=300
n$="sprite1"
sprite n$ scan x,y, w,h

graphics clear 0.5, 0.5, 1    ' make the entire screen blue (allows the scanned sprite to be distinguishable when shown)

sprite n$ at x, y
sprite n$ show

Can I draw rectangles, buttons etc on it?
You can either draw to the main graphics screen with DRAW and FILL commands, and then scan that part of the screen to a sprite, or (more conveniently) you can draw directly to the sprite, which smart Basic treats as its own graphics layer.

In this example, I removed the SPRITE SCAN and replaced it with SPRITE BEGIN and SPRITE END, which is how you draw to a sprite:

Code: Select all

graphics
graphics clear 0.5, 0.5, 1      ' screen background (blue)

x=20 ! y=500 ! w=800 ! h=300
n$="sprite1"
sprite n$ begin w,h
  graphics clear 1, 0.5, 0.5    ' sprite background (red)
  /*
  Put any draw/fill commands you want here, but recognize that coordinates
  are relative to the sprite (regardless of where the sprite is located on the
  screen). So anything drawn past the sprite's width and height will not be
  visible, just like trying to draw outside your screen's width and height on
  the main graphics window. Also, note that what you draw here will not be
  visible on screen until the sprite is visible.
  */
sprite end

sprite n$ at x, y
sprite n$ show
Last edited by matt7 on Tue Feb 19, 2019 9:44 pm, edited 2 times in total.

matt7
Posts: 115
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone
Location: USA

Re: Sprite scan

Post by matt7 »

One more note after rereading your last question: Interface objects, such as buttons, do not count as a drawing command (which are all the DRAW and FILL commands). Interface objects are "created," not drawn, so you can't "draw" a button to a sprite. They are their own entities that reside on pages along with sprites. From my quick testing just now, it appears that interface objects always appear above sprites. Even if you create an interface object inside of a SPRITE BEGIN...SPRITE END block, the object will be created as an independent entity separate from the sprite.

Post Reply