Scripting Tutorial - Lesson 11: Advanced: Introducing Classes

Similar documents
Scripting Tutorial - Lesson 9: Graphical Shape Numbers

Scripting Tutorial - Lesson 14: (Advanced) Using Keyboard Controls with Multiple Classes

Scripting Tutorial - Lesson 2

Exploring Transformations

Polygons and Angles: Student Guide

Fixed Perimeter Rectangles Geometry Creating a Document

Translations Lesson Bundle

Exploring the Equation of a Circle

Linear-Quadratic Inequalities

SCRATCH MODULE 3: NUMBER CONVERSIONS

Addition and Subtraction of. Rational Numbers Part 2. Name. Class. Student Activity. Open the TI-Nspire document Add_Sub_Rational_Numbers_Part2.tns.

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Reflections and Rotations TEACHER NOTES

Pythagorean Relationships TEACHER NOTES

Lesson 18: Animation. Computer Programming is Fun!

Transformations: Reflections

Introduction to Flash - Creating a Motion Tween

Sum of Exterior Angles of Polygons TEACHER NOTES

Linear Inequalities: Rays and Half-Planes

How to...create a Video VBOX Gauge in Inkscape. So you want to create your own gauge? How about a transparent background for those text elements?

We ve Got You Covered: 2D Area

Instructions for Crossword Assignment CS130

Topic: Conics Verify the solution to a linear-quadratic system of equations by graphing and using Intersection Point(s).

The Mailbox MATH NSPIRED. Math Objectives. About the Lesson. TI-Nspire Navigator System. Activity Materials TEACHER NOTES

Maximizing the Area of a Garden

CS 2110 Fall Instructions. 1 Installing the code. Homework 4 Paint Program. 0.1 Grading, Partners, Academic Integrity, Help

Families of Functions

TEACHER NOTES MATH NSPIRED

TImath.com. Geometry. Reflections and Rotations. Time required 45 minutes ID: 8286

HO-1: INTRODUCTION TO FIREWORKS

Transformations: Rotations /G. TEACHER NOTES MATH NSPIRED. Math Objectives. Vocabulary. About the Lesson

Adobe Illustrator. Always NAME your project file. It should be specific to you and the project you are working on.

Proof of Identities TEACHER NOTES MATH NSPIRED. Math Objectives. Vocabulary. About the Lesson. TI-Nspire Navigator System

How Many Solutions 2 TEACHER NOTES

Inequalities. Kim Mullins Grade level: 5-8 Subject: mathematics Time required: 90 minutes

Image Optimisation. Image Dimensions...1 Image File Formats...2 Adding Images...5 Thumbnails...13 Summary...15

Characteristics of Exponential Functions

Creating a Template in WordPerfect

The Fundamentals. Document Basics

HO-FL1: INTRODUCTION TO FLASH

The Environment. Scratch Programming. The Environment. The Environment. The Environment. The Environment. Lesson 1: Introduction THE STAGE

Adobe Illustrator. Quick Start Guide

In this lesson you are going to create a drawing program similar to Windows Paint. 1. Start with a new project and remove the default cat sprite.

Learning Photoshop Elements Step-by-step Lesson Two

Select, Move, Rotate and Scale

Standardized Tests: Best Practices for the TI-Nspire CX

Excel Basics Fall 2016

Adobe Flash CS3 Reference Flash CS3 Application Window

Learning to use the drawing tools

Graphic Design & Digital Photography. Photoshop Basics: Working With Selection.

1. Multimedia authoring is the process of creating a multimedia production:

Microsoft Office Word 2010

Drawing Tools. Drawing a Rectangle

USER GUIDE MADCAP CAPTURE 7. Getting Started

The Function Elevator

WAYLAND FREE PUBLIC LIBRARY 3D Design and Printing Tutorial: Create a Keychain

Mastering Truspace 7

Click the buttons in the interactive below to become familiar with the drawing tool's commands.

Animations that make decisions

Dissolving Models with Particle Flow and Animated Opacity Map

Layers (Just the Basics) By Jerry Koons

Active Images. Dick Bronson - R R -C irk its, I n c. Active images. Direct layout control (PP-clinic-2)

Navigating in SPSS. C h a p t e r 2 OBJECTIVES

Tangents. In this tutorial we are going to take a look at how tangents can affect an animation.

Irrational Thoughts. Aim. Equipment. Irrational Investigation: Student Activity

All textures produced with Texture Maker. Not Applicable. Beginner.

Interactive Tourist Map

The Unit Circle. Math Objectives. Vocabulary continuous function periodic function unit circle. About the Lesson. Related Lessons

The options for both the Rectangular and Elliptical Marquee Tools are nearly identical.

Building Concepts: Moving from Proportional Relationships to Linear Equations

SciGraphica. Tutorial Manual - Tutorials 1and 2 Version 0.8.0

SNOWFLAKES PHOTO BORDER - PHOTOSHOP CS6 / CC

Note: Photoshop tutorial is spread over two pages. Click on 2 (top or bottom) to go to the second page.

STUDENT LESSON A1 Introduction to Object-Oriented Programming (OOP)

Polygon Modeling Basics Chapter 1 - Vertices

OnPoint s Guide to MimioStudio 9

Installing a Custom AutoCAD Toolbar (CUI interface)

JavaFX Technology Building GUI Applications With JavaFX - Tutorial Overview

Ad Muncher's New Interface Layout

MODULE 1: Getting Started with Windows 10

Kidspiration Quick Start Tutorial

HAPPY HOLIDAYS PHOTO BORDER

[ the academy_of_code] Senior Beginners

HYPERSTUDIO TOOLS. THE GRAPHIC TOOL Use this tool to select graphics to edit. SPRAY PAINT CAN Scatter lots of tiny dots with this tool.

Rational Numbers and the Coordinate Plane

Step 1: Create A New Photoshop Document

TI-Nspire Technology Version 2.1

Comparing Linear and Exponential Expressions ALGEBRA NSPIRED: EXPONENTIAL FUNCTIONS

Position and Piecewise Velocity

Generations Monograms + Monogramming Masterpieces By Bernadette Griffith Generations Software

Functions and Inverses

Lesson 1. Importing and Organizing Footage using Premiere Pro CS3- CS5

Permission to copy The CAD Academy

QUICK EXCEL TUTORIAL. The Very Basics

Expression Design Lab Exercises

TI-SmartView CE Emulator Software

SOLIDWORKS: Lesson 1 - Basics and Modeling. Introduction to Robotics

Function Grapher Demystified Step 1

A Guide to Processing Photos into 3D Models Using Agisoft PhotoScan

Art, Nature, and Patterns Introduction

Transcription:

Home TI-Nspire Authoring TI-Nspire Scripting HQ Scripting Tutorial - Lesson 11 Scripting Tutorial - Lesson 11: Advanced: Introducing Classes Download supporting files for this tutorial Texas Instruments TI-Nspire Scripting Support Page In lesson 9 and lesson 10 we created a workable document for visualizing shape numbers. Like many of the documents before this, the user controls the action using arrow keys, enter, escape and tab keys. This sort of keyboard control works very well when using the handheld - it can mean that there is no need for students to have to grab and drag anything - they just start using arrow keys and the result is immediate. We have already noted, however, that this approach is useless if the document is inted for use with the Player. We have retained TI-Nspire native sliders to support such use, but this writing of variables back and forth between Lua and Nspire is probably not the most efficient way to work in terms of performance and ease of use. In fact, you might have noticed that a pretty important UI component has been missing in our introduction to Lua to this point - how can we use Lua to control and respond to mouse actions? Clearly, this is the preferred way of operating when using

a computer (as opposed to the handheld). Wouldn't it be ideal if documents we developed were actually able to be optimised for all platforms - supporting keyboard control for easy handheld access, and also working with mouse control for use with computers? As something of a bonus, if we no longer need Nspire sliders, then we probably no longer need to transfer variables and can work entirely within Lua, which must be a simpler approach for most problems. Click to see what such a document might look like Click to Play Click on the screen shot shown to view a short video of a document created in this way. Then try it using the TI-Nspire Player by

clicking on the red button beneath the image. In order to realize this goal, we need to move into the next level of Lua scripting and introduce the important and powerful tool of classes. Back to Top Lesson 11.2: A Class of its Own We will begin with something a little less ambitious. Study the video opposite and have a play with the document using the Player. You will see that it is simply two shapes which can be grabbed and dragged around using the mouse - but these

shapes can also be selected using TAB and moved using the arrow keys. The shapes, circle and square, have been defined as classes. For the moment, think of a class as more or less a "superfunction". Just as we have used functions previously to define all sorts of useful things, that is what we will do with our square and circle. But the power of classes lies in the fact that they bring with them some useful bonus properties. For example, an object defined in this way knows where it is on the screen, it knows Click to see a short video demonstration Click to Play

whether it has been selected or not, and what color it is meant to be, along with potentially much more. It can tell if it contains another object or coordinate position. Can you begin to see how this could be useful? Back to Top Lesson 11.3: Class: init? Begin by defining the empty class, Square. Next, the class must be initialized. The various properties that this class is to possess are defined here. Our class Square has position (x and y coordinates), dimensions (width and height), color and the knowledge of whether it has been selected or not (this will become clearer soon.) If we are to control the position of our

Square with a mouse, then we need to know when we click inside the Square. This is defined by the "contains" function. The function "contains" takes as input an ordered pair (x, y) and returns a Boolean value true or false if the ordered pair falls within the bounds of the Square. Finally, we need to paint the Square to the screen. In the usual way, this will require graphics context commands (gc). The first defines the color for this object, and an interesting approach is used here. At the beginning of the script, color is defined as a table as follows: Color = { } red = {0xFF, 0x00, 0x00}, green = {0x00, 0xFF, 0x00}, Since the color of Square has already been specified (in the init function), Square = class() function Square:init(x, y, width, height) self.x = x self.y = y self.width = width or 20 self.height = height or 20 self.color = Color.green self.selected = false function Square:contains(x, y) local sw = self.width local sh = self.height return x >= self.x - sw/2 and x <= self.x + sw/2 and y >= self.y - sh/2 and y <= self.y + sh/2 function Square:paint(gc) gc:setcolorrgb(unpack(self.color)) gc:fillrect(self.x - self.width / 2, self.y - self.height / 2, self.width, self.height) if self.selected then gc:setpen("medium","smooth") gc:setcolorrgb(0, 0, 0)

the unpack command simply grabs the RGB definition for green from the table, color. (In more detail: The "unpack" function takes as input a table and returns each table element as multiple return values. "gc:setcolorrgb" expects three parameters for red, green, and blue, but Color.green is one value, a table of three elements. "unpack" turns the elements of the table into the three parameters expected by setcolorrgb.) gc:drawrect(self.x - self.width / 2, self.y - self.height / 2, self.width, self.height) Draw the square in the usual way - notice the "self" references used throughout these definitions. This is a simple and effective way for a class object to refer to its own properties. Finally, a little routine that draws a black border around the square IF it is selected. Neat. Back to Top So now how do we see this Square that we have defined? First, we need to actually call the function Square

along with some parameters. Remember that the init routine required x and y coordinates, width and height (even though these last will be the same for a square). Then all that remains is to use the old on.paint(gc) function and to call the paint routine that we have defined for this class. We now have our square displayed. NOTE that it will, at present, just sit and look at you - we have not scripted any instructions to make things happen just yet. Sq = Square(80, 80, 40, 40) function on.paint(gc) Sq:paint(gc) Next we will learn how to make things happen with it. Back to Top This seems like a reasonable place to stop for this tutorial. We have introduced this key idea of classes and shown how we might create and display something in this way. Before the next lesson you might try this out and then define your own class to draw a red circle. In our next lesson we will see how to control such an object using mouse commands Home TI-Nspire Authoring TI-Nspire Scripting HQ Scripting Tutorial - Lesson 11