Академический Документы
Профессиональный Документы
Культура Документы
Contents
The AppCobra API............................................................................................................................1
Contents............................................................................................................................................1
The API...............................................................................................................................................5
What is the API?.............................................................................................................................5
A Simple API Example to Try..........................................................................................................8
Writing Your Own Routines.............................................................................................................9
Text to Appear After the <BODY> tag............................................................................................9
Text to Appear Before the </BODY> tag........................................................................................9
Formatting.......................................................................................................................................9
JavaScript.....................................................................................................................................10
The <SCRIPT> and </SCRIPT> tags...........................................................................................11
Creating your Own Routines.........................................................................................................11
Navigation and Messages..............................................................................................................13
Showing a Message (simple)........................................................................................................13
Showing a Message (advanced)..................................................................................................13
Moving Ahead Single Steps..........................................................................................................13
Moving Back One Steps...............................................................................................................13
Moving Ahead Multiple Steps.......................................................................................................13
Moving Back Multiple Steps..........................................................................................................13
Move Back After Showing Page Corner Animation......................................................................13
Move Forwards After Showing Page Corner Animation...............................................................13
Reload App without Saving User Progress..................................................................................13
Reload App and Saving User Progress........................................................................................14
Launching a URL in a new Window..............................................................................................14
Restarting App..............................................................................................................................14
Replay Step..................................................................................................................................14
Closing App...................................................................................................................................14
Print Current Step.........................................................................................................................14
Set App Status to Complete.........................................................................................................14
Set App Status to Complete But Do Not Submit..........................................................................14
Hide the Keyboard........................................................................................................................14
Generate a random number.........................................................................................................14
Variables..........................................................................................................................................15
Getting User Name.......................................................................................................................15
Getting Step Number....................................................................................................................15
Getting Total Number of Steps.....................................................................................................15
Getting Current Zoom...................................................................................................................15
Determining Browser in Use.........................................................................................................16
App Title........................................................................................................................................16
Screen Width................................................................................................................................16
Screen Height...............................................................................................................................16
Available Screen Width.................................................................................................................17
Available Screen Height...............................................................................................................17
User Results.................................................................................................................................17
Checking if the App is Being Run at Full Screen..........................................................................17
getxtag(object)..............................................................................................................................25
getxtag2(object)............................................................................................................................25
getxtag3(object)............................................................................................................................25
xgettext(object).............................................................................................................................25
imagesource(object).....................................................................................................................25
isvis(object)...................................................................................................................................25
xgetref(object)...............................................................................................................................25
overlapx(object1,object2)..............................................................................................................25
Quick Way to Set Event for an Object..........................................................................................26
Getting and Settings Values of Fields..........................................................................................27
Getting the Value of a Freeform Text Entry Field.........................................................................27
Setting the Value of a Freeform Text Entry Field.........................................................................27
Getting the Value of a Date Picker Field......................................................................................27
Setting the Value of a Date Picker Field.......................................................................................27
Getting the Value of a Fill In the Blanks Field...............................................................................27
Setting the Value of a Fill In the Blanks Field...............................................................................27
Getting the Value of a Drop Down List Object..............................................................................27
Referencing Variables Inside Text Objects.................................................................................28
Defining Variables.........................................................................................................................28
Using the Variable.........................................................................................................................28
Unexpected Values.......................................................................................................................30
Using the Clock...............................................................................................................................31
Setting the Time............................................................................................................................31
Getting the Time ..........................................................................................................................31
Using the Calendar.........................................................................................................................32
Checking the Date........................................................................................................................32
Checking the Selected Date.........................................................................................................32
Setting the Date............................................................................................................................32
Canvas Objects...............................................................................................................................33
Transfer an Image to the Canvas.................................................................................................33
Transfer an Image from one Canvas object to another................................................................33
Transfer an Image from a Canvas object to an Image object......................................................33
Custom Draw an Image onto a Canvas........................................................................................33
Trim an Image Object...................................................................................................................33
Trim a Canvas Object...................................................................................................................33
Convert a Canvas to Grayscale....................................................................................................33
Change the Brightness of a Canvas.............................................................................................33
Change the Contrast of a Canvas................................................................................................34
Change the Saturation of a Canvas.............................................................................................34
Changing Red, Green and Blue values of a Canvas....................................................................34
Change the Sharpness of a Canvas.............................................................................................34
Save a Canvas as an Image.........................................................................................................34
Save a Canvas into Local Storage...............................................................................................34
Load an Image from Local Storage into an Image.......................................................................34
Prevent / Allow Drawing................................................................................................................34
Events..............................................................................................................................................35
Events Routines..............................................................................................................................35
When an Object has Finished Moving..........................................................................................35
The API
What is the API?
The API is essentially a series of JavaScript routines you can execute in place of standard
JavaScript events.
Let's look at an example. Say we have a picture on the screen.
We select this image. Now we select the Interact / All Events command.
In the When listbox, we have Object is Pressed, so this will execute when the mouse is
pressed.
In the Perform This Action listbox, we'll select the JavaScript Routine option.
In the Routine to Run, we are going to add a routine that displays a message box.
We enter this text:
xmessage("This is my message.")
It appears like this:
When we run the app, and click on the textbox, here is what we see:
It is that simple. All you need to know is the possible routines you can run, and these are listed
below.
A new way to do the same thing as above but a little more quickly is to select the object,
then select the Interact / Run JavaScript code command. There, you can just add code as
required. No <SCRIPT> or </SCRIPT> tags are required.
Start a new app, and add an object to the screen. Does not really matter what it is an
image, a created graphic, a button, even a text object.
Select the object, and select the Interact / Run JavaScript Code command.
In the text area, enter the AppCobra API line that reads like this:
o positionx("myimage"10,10)
Use the Preview button in the main toolbar to test this out.
When the App appears previewed in a browser, touch, or click on it. It will immediately move to
the position on screen 10 pixels from the top, and 10 pixels from the left, of the screen.
Formatting
This text you enter here should be formatted in one of two ways, depending on whether you are
adding HTML, or JavaScript code. HTML.
If you are adding HTML code, then it should be plain and simple JavaScript code. For example,
if we add some code into the Edit/Includes dialog box (like this):
<DIV style='position:absolute;left:10px;top:10px;z-index:5000;'><IMG
SRC='http://www.appcobra.com/assets/applogonew.gif'></DIV>
This is code that loads an image, places it at the 10 pixel by pixel mark on the screen, and
places it on top.
JavaScript
With JavaScript code, you have to be a little more careful. This is because it must be formatted
correctly to ensure that no errors are generated.
Here is an example. Say we add this code to the app using the Edit / Includes dialog box:
<SCRIPT>
alert ("hello")
</SCRIPT>
When we run this app, this is what will appear:
This will appear before any content actually appears on the page.
If this were added to the Text to Appear Before the </BODY> tag area, it would appear after
the content had appeared.
If we use the example we had at the start of this document, with the image, here is what we
would add in the Interact / Run JavaScript Code dialog box for that image. When we run this
app, here is what we see when we click on the object:
Restarting App
Syntax: restart()
Will restart the app.
Replay Step
Syntax: watchstepagain()
Will play the current step again, with all appearance and disappearance delays and animations.
Closing App
Syntax: closeit()
Will close the app immediately (if running in a web browser). Does nothing if actually running as
an app (apps cannot close themselves).
Variables
In many cases, there are a range of variables that can be used. These might be used to alter
what displays in a message box, to whether a command is executed or not. For example let's
assume you want to display to someone their screen resolution. You could construct a
JavaScript event that looks like this:
xmessage("Your screen resolution is " + screen.width + " x " + screen.height + ".")
When this event is executed, it will display something like this:
This is explained in a little more detail below. Using variables requires exact syntax, or error
messages will occur.
Below, we list some of the variables available within AppCobra, plus some common JavaScript
variables.
Actually, any JavaScript variable can be used.
App Title
Syntax: document.title
Returns the name of the app.
Screen Width
Syntax: screen.width
Returns the width of the screen in pixels.
Screen Height
Syntax: screen.height
User Results
Syntax: getresults()
Returns all user results in the app.
from the left, and 100 pixels from the top of the screen.
Always include the object name in quotes! The example below will result in a
WRONG!
positionx("aliasname",100,100)
RIGHT!
Milliseconds
Many routines below involve some sort of timing. For example:
hidexdelay(object, x)
Will hide an object after a delay of x milliseconds. 1000 milliseconds is 1 second. So
hidexdelay("aliasname",1000)
Will hide an object after 1000 milliseconds, or one second.
hidexdelay("aliasname",5000)
Will hide an object after 5000 milliseconds, or five seconds.
hidexdelay("aliasname",1)
Will hide an object after 1 millisecond or 1/1000 of a second or essentially, immediately!
Positioning an Object
positionx(object,x,y)
Positions object to x,y pixels
centerx(object,x,y)
Centers object around x,y pixels
xalign(object,align)
Aligns object to align, relative to canvas. Options are 'topleft', 'topcenter', 'topright', 'bottomleft',
'bottomcenter', 'bottomright', 'center', 'centerleft', 'centerright'
xrestorepos(object)
Restores object to original position
setoffset(object,x,y)
Moves object to new position which is x and y pixels from current position. Can be negative
values as well.
xrandompos(object,left,top,right,bottom)
Randomly positions an object within the given coordinates.
Move Objects
xmoveto(object,fromx,fromy,tox,toy,delay,totaltime)
Moves an object from fromx,fromy to tox,toy (pixels), at delay intervals (milliseconds).
Totaltime is total effect time in milliseconds
offbottomx(object,timedelay,stepdelay)
Moves an object off bottom of screen, at timedelay intervals, at stepdelay increments, and
hides it
offtopx(object,timedelay,stepdelay)
Moves an object off top of screen, at timedelay intervals, at stepdelay increments, and hides it
offleftx(object,timedelay,stepdelay)
Moves an object off left of screen, at stepdelay intervals, at stepdelay increments, and hides it
offrightx(object,timedelay,stepdelay)
Moves an object off right of screen, at stepdelay intervals, at stepdelay increments, and hides
it
wobblex(delay,object,offset,totaltime,timeinterval)
Wobbles an object on the screen. Delay is milliseconds before wobble begins. Offset is pixel
number to wobble over. Totaltime is milliseconds of total event duration. Timeinterval is in
milliseconds and represents time between each wobble.
Rotate an Object
xrotate(object,degrees)
Rotates object to x degrees plus current object rotation
xrotateto(object,from,to,delay,totaltime)
Rotates an object from from(%) to to(%), at delay intervals (milliseconds). Totaltime is total
effect time in milliseconds
xrotatefixed(object,degrees)
Rotates object to x degrees
Update an Object
updatexobject(object)
Updates an object where a variable has changed value
xgetstate(object)
returns either "up" or "down", depending on the current state of object.
xrestorestate(object)
returns object to its normal, or up state
xdownstate(object)
Sets object to its down state
xswapstate(object)
Swaps the current state of object.
Accelerometer Routines
Set Movement Coordinates
xsetdragcoords(object,left,top,right,bottom)
Sets the area in which the object being dragged can only be dragged within.
Dragging Routines
Allow Dragging
allowdragx(object)
Allows an object to be dragged around the screen
Prevent Dragging
preventdragx(object)
Prevents an object from being dragged around the screen
getxrotation(object)
Returns rotation of object (degrees)
getxscale(object)
Returns scale of object (where 1 is original size)
getxleft(object)
Returns left position of object in pixels
getxtop(object)
Returns top position of object in pixels
getxwidth(object)
Returns width of object in pixels
getxheight(object)
Returns height of object in pixels
getxtag(object)
Returns value of tag1 for object
getxtag2(object)
Returns value of tag2 for object
getxtag3(object)
Returns value of tag3 for object
xgettext(object)
Returns text of object
imagesource(object)
Returns image source for object
isvis(object)
Returns true or false based on whether object is visible or not
xgetref(object)
Returns internal reference of object
overlapx(object1,object2)
Returns true or false on whether the object called object1 overlaps the object called object2
(centrally), and both are visible.
If you referenced a variable that you have not created, or misspell a variable name (variables in
JavaScript are case sensitive), the screen would look like this.
When a variable in the format %%%variable%%% is used, it is evaluated each time a step is
changed. So, you can change the value of the variable just remember that it will not update in
the text object until the step changes. Shortly, you'll see there is a routine you can run that will
update any object that contains a variable:
updatexobject(object)
Unexpected Values
If the text box displays an unexpected value say 18 instead of myvalue value, for example, it
may mean that you have used one of the reserved variables used by the AppCobra app itself.
Such variables include score, possiblescore, pause, pauses, noerrors, closemethod, etc.
Canvas Objects
The following canvas API calls assume that you have added a canvas object to your app.
Events
There are several events that will be triggered under certain circumstances.
As these are events, they are generated by the app itself. So, there is a little extra something
you need to add.
For example, let us say that you want something to happen when the user gets to step 3 in your
app. Normally, you could just add something to step 3 in your app, but for this example let us
say we want to display a message when the user reaches step 3.
To do this, we need to add the routine that corresponds to the event we want to respond to,
using the Edit / Includes command. In this case, we would have to add (in the Code to Appear
before the </BODY> tag section):
<SCRIPT>
function stepchangex(t){
}
</SCRIPT>
Adding this routine allows us to add whatever JavaScript, or AppCobra API code we like to
occur at step 3. For example, this code will display a message when the user gets to step 3:
<SCRIPT>
function stepchangex(t){
if (t==3){
xmessage("You are on step 3!")
}
}
</SCRIPT>
In this routine, the variable t represents the current step number.
Here is something slightly smarter. Let us assume we want to display a message when they get
to step 3, but only the very first time they get to step 3. This code might look like this:
<SCRIPT>
var messageshown=false
function stepchangex(t){
if (t==3&&messageshown==false){
xmessage("You are on step 3!")
messageshown=true
}
}
</SCRIPT>
Here, we've created a variable called messageshown. When the user gets to step 3, we check
to see whether this variable is set to true, or false. If false, we show the message, and set the
value of the variable to true. From now on, this message will not display, even if the user comes
back to Step 3 at a later stage.
Events Routines
When an Object has Finished Moving
xmovedone(object)
is executed when a move is completed
Respond to a Keypress
function keypressx(t){}
This occurs when a keyboard button is pressed. t is the keyboard button.
if (okpressed){
//this means the user has clicked on OK.
}
}
checkingfordeleteok=false
}
If you have use messageboxes a lot, you will find the customx routine will end up looking
something like this:
//first, we declare the variables we are going to use to check messagebox results
var checkingfordeleteok=false
var checkingtoaddfile=false
varcheckingtoclosedown=false
function customx(){
if (checkingfordeleteok==true){
if (okpressed){
//the user has clicked on OK in response to the checking to delete a file
question.
}
}
if (checkingtoaddfile==true){
if (okpressed){
//the user has clicked on OK in response to the checking to adding a file
question.
}
}
if (checkingtoclosedown==true){
if (okpressed){
//the user has clicked on OK in response to the checking to closing a file
question.
}
}
checkingfordeleteok=false
checkingtoaddfile =false
checkingtoclosedown =false
}
In this case, we add, as we've done above, the JavaScript event myroutine() to an object:
When the user clicks on this object, the routine you've written called myroutine() executes.
Now, in this routine, we added some logic (highlighted in red):
<SCRIPT>
function myroutine(){
if (getstepnumber()==1){
alert ("hello")
}
}
</SCRIPT>
Here, using standard JavaScript notation, we are in effect saying if the user is looking at Step
1, then display an alert. Otherwise don't do anything.
How inventive you get depends on you. Here is another, slightly more complex example.
<SCRIPT>
function myroutine(){
if (getstepnumber()==1){
alert ("You are looking at Step 1.")
}else{
If (getstepnumber()=getduration()){
alert ("You are looking at Step " + getstepnumber() + ", last step in the app.")
}
}
}
</SCRIPT>
In this case, the user will see a message that reads You are looking at Step 1. if they are on
step 1, and a message that reads You are looking at Step 10, the last step in this app.
(assuming Step 10 was the last step in that app).
:
In this example, we've added an arrow that points up, and an arrow that points down. There are
some special events that allow you to let these arrows control scrolling in this web object.
For this example, we assume that the actual web page object is called appcobra. To confirm
this, select the main web object, and use the Objects/Alias command to ensure it is called
appcobra
We've done two things first, we select the option JavaScript Routine from the Perform This
Action listbox. Then, in the Routine to Run: area, we added the text:
xscrollup("appcobra")
where appcobra is the web object we want to control, and xscrollup can be either xscrollup,
xscrolldown, xscrollright, or xscrollleft.
Once you've applied any combination of these events, AppCobra will do the rest. That object
will then let you scroll the web object up, down, left or right.
So, you could add a routine in your app (using the Edit/Includes command) that reads
something like this:
var cm=setInterval('checkmovement()',20)
function checkmovement(){
currentxmovement=_kpmx*
}
*here you put any code you like. But you can reference any of the above variables to move or
respond in any way you like.
If you are using any of the moveme objects to be automatically moved around the screen, each
time they are moved, a routine called objectsmoved() is called. So, to respond to these objects
moving, you'd have to add this routine manually, using the Edit/Includes command:
function objectsmoved(){
//add your own code
}
geolocatex
geolocatey
geoaccuracy
geolocatex represents the user longitude, geolocatey represents the user latitude, and
geoaccuracy is a figure that represents how accurately the location was determined to.
Initially, geolocatex and geolocatey are set to the string 'Undetermined', until such time as
location is determined.
If there is any error determining location, or the user refuses the request, both values are set to
the string 'null'.
Setting a Value
To save some content to a user machine, use this syntax:
xwritedata("myinfo", "Information I want to store")
In this example, the string "Information I want to store" is stored in a variable called
localstorage["myinfo"]. Unless the user specifically removes this from their machine, it will be
available any time the same page loads. So, when your app loads a subsequent time, you can
read this information again see below.
Retrieving a Value
Using the syntax:
theinfo= xreaddata(xname)
The variable called theinfo will be filled with the value stored under this variable see above.
function listdeleted(t){
//check for value of t. This tells you the delete button was clicked in what line
}
Handling Audio
The following routines will only work for objects where audio has been loaded.
soundxduration(object)
Returns duration (milliseconds) of sound object
stopsoundx(object)
Stops sound playing of object.
pausesoundx(object, tao)
Pauses sound.
playsoundx(object)
Plays sound.
setsoundposx(object,xval)
Sets sound position in xval milliseconds of object
volumesoundx(object,xval)
Sets volume of audio object.