d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help With Functions
Add Reply New Topic New Poll
Member
Posts: 16,404
Joined: Mar 28 2009
Gold: 7.69
Sep 22 2016 04:32pm
So basically I have these 2 sets of code. One is supposed to be a superhero and the other is a villain (im not artsy). For this new assignment, what I need to do is take both sets of code and encapsulate them into function(s) onto a whole new blank document...so both the villain and superhero are on the same page. I'm unsure where to start.

Code
var xPos = 250;
var yPos = 200;

var x_one = 210;
var y_one = 230;

var x_two = 250;
var y_two = 200;

var x_three = 290;
var y_three = 230;

draw = function() {
background(113, 230, 230);

//grass
noStroke();
fill(25, 204, 61);
rect(0, 300, 410, 300);

//cape
fill(255, 0, 0);
triangle(x_one, y_one, x_two, y_two, x_three, y_three);

//ghost hero
fill(20, 18, 18);
ellipse(xPos, yPos, 30, 30);

xPos = xPos-2;
yPos = yPos-2;

x_one = x_one-2;
y_one = y_one-2;

x_two = x_two-2;
y_two = y_two-2;

x_three = x_three-2;
y_three = y_three-2;

};



Code
noStroke();
background(8, 8, 8);

//moon
fill(196, 188, 188);
ellipse(50,50,50,50);

//appendage
fill(255, 0, 0);
stroke(0, 89, 255);
rect(153,185,45,150);

//head
triangle(100,185,250,185,174,100);

//eyes
stroke(13, 28, 4);
fill(145, 127, 47);
ellipse(160,140,15,15);
ellipse(190,140,15,15);

//mouth
noFill();
line(160,165,190,165);

//ears
fill(179, 16, 16);
ellipse(138,120,25,35);
ellipse(211,120,25,35);


This post was edited by Shakti on Sep 22 2016 04:34pm
Member
Posts: 16,404
Joined: Mar 28 2009
Gold: 7.69
Sep 22 2016 05:06pm
Cool, so I figured out how to call them both with a function. Now the problem that persists is getting them to fly. Not sure how to put the draw = function(); inside of my superhero function.. thats like a function within a function? Is that possible?

Code
var xPos = 250;
var yPos = 200;

var x_one = 210;
var y_one = 230;

var x_two = 250;
var y_two = 200;

var x_three = 290;
var y_three = 230;

var superhero = function() {
background(0, 128, 255);

//grass
noStroke();
fill(25, 204, 61);
rect(0, 300, 410, 300);

//cape
fill(255, 0, 0);
triangle(x_one, y_one, x_two, y_two, x_three, y_three);

//ghost hero
fill(20, 18, 18);
ellipse(xPos, yPos, 30, 30);

xPos = xPos-2;
yPos = yPos-2;

x_one = x_one-2;
y_one = y_one-2;

x_two = x_two-2;
y_two = y_two-2;

x_three = x_three-2;
y_three = y_three-2;

//appendage
fill(255, 0, 0);
stroke(0, 89, 255);
rect(25,176,18,85);

//head
triangle(5,176,66,176,34,133);

//eyes
stroke(13, 28, 4);
fill(145, 127, 47);
ellipse(29,155,7,7);
ellipse(42,155,7,7);

//mouth
noFill();
line(21,165,47,165);

//ears
fill(179, 16, 16);
ellipse(17,149,7,12);
ellipse(51,147,7,12);



};
superhero();



God yes i got it :D



This post was edited by Shakti on Sep 22 2016 05:10pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll